/*******************************************************************************
 *     ___                  _   ____  ____
 *    / _ \ _   _  ___  ___| |_|  _ \| __ )
 *   | | | | | | |/ _ \/ __| __| | | |  _ \
 *   | |_| | |_| |  __/\__ \ |_| |_| | |_) |
 *    \__\_\\__,_|\___||___/\__|____/|____/
 *
 *  Copyright (c) 2014-2019 Appsicle
 *  Copyright (c) 2019-2024 QuestDB
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

package io.questdb.test.griffin.engine.groupby;

import io.questdb.PropertyKey;
import io.questdb.cairo.CairoConfiguration;
import io.questdb.cairo.CairoEngine;
import io.questdb.cairo.CairoException;
import io.questdb.cairo.ColumnType;
import io.questdb.cairo.CursorPrinter;
import io.questdb.cairo.MicrosTimestampDriver;
import io.questdb.cairo.TableWriter;
import io.questdb.cairo.sql.Record;
import io.questdb.cairo.sql.RecordCursor;
import io.questdb.cairo.sql.RecordCursorFactory;
import io.questdb.cairo.sql.RecordMetadata;
import io.questdb.griffin.SqlCompiler;
import io.questdb.griffin.SqlException;
import io.questdb.griffin.SqlExecutionContextImpl;
import io.questdb.log.Log;
import io.questdb.log.LogFactory;
import io.questdb.mp.SOCountDownLatch;
import io.questdb.mp.WorkerPool;
import io.questdb.std.Chars;
import io.questdb.std.FilesFacade;
import io.questdb.std.Misc;
import io.questdb.std.Os;
import io.questdb.std.datetime.nanotime.Nanos;
import io.questdb.std.str.Path;
import io.questdb.std.str.StringSink;
import io.questdb.test.AbstractCairoTest;
import io.questdb.test.cairo.DefaultTestCairoConfiguration;
import io.questdb.test.cutlass.text.SqlExecutionContextStub;
import io.questdb.test.std.TestFilesFacadeImpl;
import io.questdb.test.tools.TestUtils;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.TimeZone;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import static io.questdb.test.tools.TestUtils.assertEquals;

public class SampleByNanoTimestampTest extends AbstractCairoTest {
    public static final String FROM_TO_DDL = """
            create table fromto as (
              SELECT timestamp_sequence_ns(
                        to_timestamp_ns('2018-01-01T00:00:00', 'yyyy-MM-ddTHH:mm:ss'),
                        1800000000000L) as ts, \
            x, \
            x::varchar as s,\
            x::byte as b,\
            x::short as e,\
            x::int as i,\
            x::long as l,\
            x::float as f,\
            x::double as d,\
            x::string as str,\
            x::char as a,\
            x::symbol as k,\
            x::boolean as t,\
            x::timestamp_ns as n,\
            FROM long_sequence(480)
            ) timestamp(ts)""";
    private static final Log LOG = LogFactory.getLog(SampleByNanoTimestampTest.class);

    @Test
    public void testBadFunction() throws Exception {
        assertException(
                "select b, sum(a), sum(c), k from x sample by 3h fill(20.56)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(172800000000::timestamp_ns, 3600000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                22,
                "Invalid column: c"
        );
    }

    @Test
    public void testBadInterval() throws Exception {
        assertException(
                "select b, sum(a), k from x sample by 1hour",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000, 3600000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                37,
                "Invalid unit: 1hour"
        );
    }

    @Test
    public void testBindVarsInPeriodSyntax() throws Exception {
        testSampleByPeriodFails(
                "select k, s, first(lat) lat, last(lon) lon from x where s in ('a') sample by $1 T align to calendar",
                "select k, s, first(lat) lat, last(lon) lon from x where s in ('a') sample by $".length() - 1,
                "sample by period must be a constant expression"
        );
    }

    @Test
    public void testFillPrevConsistency() throws SqlException {
        execute("create table telem (created timestamp_ns, event_type int, table_id int, latency double) timestamp(created) partition by DAY");

        execute("insert into telem " +
                "select" +
                " timestamp_sequence_ns('2025-01-20T13:57:14.000000000Z', 2500000)," +
                " rnd_int() % 4," +
                " rnd_int() % 100," +
                " abs(rnd_double())" +
                " from long_sequence(100_000)");
        assertQueryNoLeakCheck(
                """
                        created\tavg\tlatency
                        2025-01-20T13:56:50.000000000Z\t0.0\t0.0
                        2025-01-20T13:56:52.000000000Z\t0.0\t0.0
                        2025-01-20T13:56:54.000000000Z\t0.0\t0.0
                        2025-01-20T13:56:56.000000000Z\t0.0\t0.0
                        2025-01-20T13:56:58.000000000Z\t0.0\t0.0
                        2025-01-20T13:57:00.000000000Z\t0.0\t0.0
                        2025-01-20T13:57:02.000000000Z\t0.0\t0.0
                        2025-01-20T13:57:04.000000000Z\t0.0\t0.0
                        2025-01-20T13:57:06.000000000Z\t0.0\t0.0
                        2025-01-20T13:57:08.000000000Z\t0.0\t0.0
                        2025-01-20T13:57:10.000000000Z\t0.0\t0.0
                        2025-01-20T13:57:12.000000000Z\t0.0\t0.0
                        2025-01-20T13:57:14.000000000Z\t0.4851638802935891\t0.4846019644078461
                        2025-01-20T13:57:16.000000000Z\t0.5040684715238979\t0.0014510055926236776
                        2025-01-20T13:57:18.000000000Z\t0.4855058436740148\t0.760595244599882
                        2025-01-20T13:57:20.000000000Z\t0.5022292093071415\t0.47033911949468266
                        2025-01-20T13:57:22.000000000Z\t0.5052664001761066\t0.8733949114345736
                        2025-01-20T13:57:24.000000000Z\t0.49787296387399244\t0.45485294118316344
                        2025-01-20T13:57:26.000000000Z\t0.5157930891201031\t0.7007778377722195
                        2025-01-20T13:57:28.000000000Z\t0.49810868200578157\t0.22896810382600008
                        2025-01-20T13:57:30.000000000Z\t0.4939448789648544\t0.77359848960926
                        2025-01-20T13:57:32.000000000Z\t0.4897555120505765\t0.8994724612954251
                        2025-01-20T13:57:34.000000000Z\t0.5017879450710985\t0.5496601327901486
                        2025-01-20T13:57:36.000000000Z\t0.4956674689326836\t0.26110257790188807
                        2025-01-20T13:57:38.000000000Z\t0.5108794030479952\t0.38345997979929847
                        2025-01-20T13:57:40.000000000Z\t0.5053240993230795\t0.520757673455773
                        2025-01-20T13:57:42.000000000Z\t0.4988663699172193\t0.7881958875290708
                        2025-01-20T13:57:44.000000000Z\t0.503737560421611\t0.4669565407466736
                        2025-01-20T13:57:46.000000000Z\t0.5099543384735293\t0.5608039955756913
                        2025-01-20T13:57:48.000000000Z\t0.4959893946518482\t0.2843815949074908
                        2025-01-20T13:57:50.000000000Z\t0.5016936986983442\t0.8505737925875698
                        2025-01-20T13:57:52.000000000Z\t0.5054365351895309\t0.12286408407144689
                        2025-01-20T13:57:54.000000000Z\t0.4906196065427902\t0.18333103699732645
                        2025-01-20T13:57:56.000000000Z\t0.5000897294717391\t0.8657117894806349
                        2025-01-20T13:57:58.000000000Z\t0.5098086517700594\t0.34100284129451275
                        2025-01-20T13:58:00.000000000Z\t0.49966159558186946\t0.12147807024033108
                        2025-01-20T13:58:02.000000000Z\t0.5004546880326035\t0.6448153122398131
                        2025-01-20T13:58:04.000000000Z\t0.509048504935009\t0.36592352333201006
                        2025-01-20T13:58:06.000000000Z\t0.50368892165536\t0.8537660947780769
                        2025-01-20T13:58:08.000000000Z\t0.5135177208750468\t0.9820930620204293
                        2025-01-20T13:58:10.000000000Z\t0.4946776746792163\t0.5371999576078124
                        2025-01-20T13:58:12.000000000Z\t0.5046786160937665\t0.4597099533579079
                        2025-01-20T13:58:14.000000000Z\t0.5104309717281234\t0.9703717493026135
                        2025-01-20T13:58:16.000000000Z\t0.5063263221664347\t0.3173787005052606
                        2025-01-20T13:58:18.000000000Z\t0.5050422491444898\t0.8321198770692697
                        2025-01-20T13:58:20.000000000Z\t0.49942269756582924\t0.15080308349106386
                        2025-01-20T13:58:22.000000000Z\t0.5001306567656737\t0.0754281163419458
                        2025-01-20T13:58:24.000000000Z\t0.49295208178613864\t0.9199510142446139
                        2025-01-20T13:58:26.000000000Z\t0.5059398643771146\t0.4660874688287775
                        2025-01-20T13:58:28.000000000Z\t0.4981379556559048\t0.30033880795051826
                        2025-01-20T13:58:30.000000000Z\t0.5000418143092644\t0.5705182422010522
                        2025-01-20T13:58:32.000000000Z\t0.5106167257086114\t0.09515699608123174
                        2025-01-20T13:58:34.000000000Z\t0.5067174084443764\t0.05834219734998902
                        2025-01-20T13:58:36.000000000Z\t0.5068112645934018\t0.1817699293870746
                        2025-01-20T13:58:38.000000000Z\t0.4988092223173822\t0.7193513157733163
                        2025-01-20T13:58:40.000000000Z\t0.5022197872758152\t0.718094867327548
                        2025-01-20T13:58:42.000000000Z\t0.5106127706294329\t0.8837745591581858
                        2025-01-20T13:58:44.000000000Z\t0.5028917099020589\t0.47476695890232523
                        2025-01-20T13:58:46.000000000Z\t0.5007363609737706\t0.13736027364505599
                        2025-01-20T13:58:48.000000000Z\t0.492334610467138\t0.5229151773456188
                        2025-01-20T13:58:50.000000000Z\t0.5001068629548213\t0.5240982714764488
                        2025-01-20T13:58:52.000000000Z\t0.4994174463473467\t0.47333881895683316
                        2025-01-20T13:58:54.000000000Z\t0.49424015140261957\t0.42701619170227834
                        2025-01-20T13:58:56.000000000Z\t0.48757211780610227\t0.08512812943026549
                        2025-01-20T13:58:58.000000000Z\t0.5100223179690243\t0.16819110732876985
                        2025-01-20T13:59:00.000000000Z\t0.49943191308339374\t0.2702475064009233
                        2025-01-20T13:59:02.000000000Z\t0.48652969691916503\t0.7998124033479126
                        2025-01-20T13:59:04.000000000Z\t0.5016355069527151\t0.7227406508693206
                        2025-01-20T13:59:06.000000000Z\t0.5072743308494192\t0.8646291312820938
                        2025-01-20T13:59:08.000000000Z\t0.5146616087916882\t0.593463629527755
                        2025-01-20T13:59:10.000000000Z\t0.4924321489143956\t0.9113233365891048
                        2025-01-20T13:59:12.000000000Z\t0.5019611647508408\t0.5287400406602393
                        2025-01-20T13:59:14.000000000Z\t0.5047832584945308\t0.7358918625777293
                        2025-01-20T13:59:16.000000000Z\t0.506063752855275\t0.5402660817376952
                        2025-01-20T13:59:18.000000000Z\t0.524139855411055\t0.6610279878811472
                        2025-01-20T13:59:20.000000000Z\t0.49536698884043756\t0.8383151117609724
                        2025-01-20T13:59:22.000000000Z\t0.523861275985825\t0.4449315194100708
                        2025-01-20T13:59:24.000000000Z\t0.5065803411918478\t0.33950559953239867
                        2025-01-20T13:59:26.000000000Z\t0.49505000311232045\t0.36271880845750437
                        2025-01-20T13:59:28.000000000Z\t0.49762813072208195\t0.7118659078648814
                        2025-01-20T13:59:30.000000000Z\t0.4914193450153209\t0.22370121110446894
                        2025-01-20T13:59:32.000000000Z\t0.49997792522902584\t0.12484119317638909
                        2025-01-20T13:59:34.000000000Z\t0.5131846146411408\t0.7627858499872365
                        2025-01-20T13:59:36.000000000Z\t0.50575819056176\t0.2989367055630815
                        2025-01-20T13:59:38.000000000Z\t0.5115672301835786\t0.7311461454026085
                        2025-01-20T13:59:40.000000000Z\t0.489770739523703\t0.364203755015714
                        2025-01-20T13:59:42.000000000Z\t0.48827098190254603\t0.6497666544419626
                        2025-01-20T13:59:44.000000000Z\t0.4919096456204459\t0.1751598713431003
                        2025-01-20T13:59:46.000000000Z\t0.5025937922857477\t0.7887957915957375
                        2025-01-20T13:59:48.000000000Z\t0.4802568230380409\t0.7425480329713459
                        2025-01-20T13:59:50.000000000Z\t0.5052745501212974\t0.7105531082982322
                        2025-01-20T13:59:52.000000000Z\t0.5153544761406945\t0.356986250439874
                        2025-01-20T13:59:54.000000000Z\t0.5226395082273337\t0.6793175726750598
                        2025-01-20T13:59:56.000000000Z\t0.5113708824401949\t0.6917192181729446
                        2025-01-20T13:59:58.000000000Z\t0.5007369251682845\t0.5566259650060899
                        2025-01-20T14:00:00.000000000Z\t0.4875623932277653\t0.41544865478057724
                        2025-01-20T14:00:02.000000000Z\t0.500581587909613\t0.35535692290002574
                        2025-01-20T14:00:04.000000000Z\t0.5033365462753482\t0.6425242900479169
                        2025-01-20T14:00:06.000000000Z\t0.49973458951836724\t0.8057830760536094
                        2025-01-20T14:00:08.000000000Z\t0.5133768100928325\t0.4832635039849207
                        2025-01-20T14:00:10.000000000Z\t0.49959821200212373\t0.07969074155053768
                        2025-01-20T14:00:12.000000000Z\t0.48631574988877824\t0.1852594496810288
                        2025-01-20T14:00:14.000000000Z\t0.4775700508803147\t0.1746040305540778
                        2025-01-20T14:00:16.000000000Z\t0.4904731973923076\t0.8319077385303282
                        2025-01-20T14:00:18.000000000Z\t0.5172686285942334\t0.4602012941029604
                        2025-01-20T14:00:20.000000000Z\t0.49438245747522913\t0.496679499909753
                        2025-01-20T14:00:22.000000000Z\t0.48890769363602105\t0.6624975451169404
                        2025-01-20T14:00:24.000000000Z\t0.524539276436426\t0.7613314616414868
                        2025-01-20T14:00:26.000000000Z\t0.47754365417732036\t0.7972715048962937
                        2025-01-20T14:00:28.000000000Z\t0.49558490815429723\t0.5650524947355062
                        2025-01-20T14:00:30.000000000Z\t0.478522476906104\t0.8980004790312893
                        2025-01-20T14:00:32.000000000Z\t0.5032895135257704\t0.8725153533702712
                        2025-01-20T14:00:34.000000000Z\t0.4885618841436868\t0.16756071058522382
                        2025-01-20T14:00:36.000000000Z\t0.4945189248409826\t0.20602018544449763
                        2025-01-20T14:00:38.000000000Z\t0.49157255178162024\t0.9762360584775526
                        2025-01-20T14:00:40.000000000Z\t0.5021366595107085\t2.2543219813986326E-4
                        2025-01-20T14:00:42.000000000Z\t0.5010926084080161\t0.9927756543484265
                        2025-01-20T14:00:44.000000000Z\t0.5061735021344332\t0.3034233576280241
                        2025-01-20T14:00:46.000000000Z\t0.49349786391999323\t0.2990105656288151
                        2025-01-20T14:00:48.000000000Z\t0.5052800074403885\t0.1118642844938007
                        2025-01-20T14:00:50.000000000Z\t0.478761125271968\t0.6763386750115261
                        2025-01-20T14:00:52.000000000Z\t0.514706027105563\t0.7763367804571444
                        2025-01-20T14:00:54.000000000Z\t0.5055529298956492\t0.29442457234943387
                        2025-01-20T14:00:56.000000000Z\t0.4750145366710726\t0.20393005251349205
                        2025-01-20T14:00:58.000000000Z\t0.4884816249911365\t0.33499512944932763
                        2025-01-20T14:01:00.000000000Z\t0.5094157389039983\t0.021272791876005726
                        2025-01-20T14:01:02.000000000Z\t0.5047514008290243\t0.5788643390071768
                        2025-01-20T14:01:04.000000000Z\t0.4863102272143384\t0.00965027758091308
                        2025-01-20T14:01:06.000000000Z\t0.5071935666235293\t0.762329563481065
                        2025-01-20T14:01:08.000000000Z\t0.5088687556412043\t0.28544640285844014
                        2025-01-20T14:01:10.000000000Z\t0.4960479410464751\t0.49922336302934456
                        2025-01-20T14:01:12.000000000Z\t0.4979687963161959\t0.8774491669657007
                        2025-01-20T14:01:14.000000000Z\t0.4959509451344056\t0.37826342574379146
                        2025-01-20T14:01:16.000000000Z\t0.51067644621747\t0.7751692205906762
                        2025-01-20T14:01:18.000000000Z\t0.49924129070780227\t0.5021223982023798
                        2025-01-20T14:01:20.000000000Z\t0.4972046530980123\t0.5263994817020877
                        2025-01-20T14:01:22.000000000Z\t0.5080393379955584\t0.26648957155568953
                        2025-01-20T14:01:24.000000000Z\t0.0\t0.26648957155568953
                        2025-01-20T14:01:26.000000000Z\t0.0\t0.26648957155568953
                        2025-01-20T14:01:28.000000000Z\t0.0\t0.26648957155568953
                        2025-01-20T14:01:30.000000000Z\t0.0\t0.26648957155568953
                        2025-01-20T14:01:32.000000000Z\t0.0\t0.26648957155568953
                        2025-01-20T14:01:34.000000000Z\t0.0\t0.26648957155568953
                        2025-01-20T14:01:36.000000000Z\t0.0\t0.26648957155568953
                        2025-01-20T14:01:38.000000000Z\t0.0\t0.26648957155568953
                        2025-01-20T14:01:40.000000000Z\t0.0\t0.26648957155568953
                        2025-01-20T14:01:42.000000000Z\t0.0\t0.26648957155568953
                        2025-01-20T14:01:44.000000000Z\t0.0\t0.26648957155568953
                        2025-01-20T14:01:46.000000000Z\t0.0\t0.26648957155568953
                        2025-01-20T14:01:48.000000000Z\t0.0\t0.26648957155568953
                        2025-01-20T14:01:50.000000000Z\t0.0\t0.26648957155568953
                        """,
                """
                        select created, avg(latency) avg, last(latency) latency
                          from telem
                          sample by 2s
                          FROM timestamp_floor('2s', '2025-01-20T13:56:50Z')
                          TO timestamp_floor('2s', '2025-01-20T14:01:52Z')
                          fill(0,prev);""",
                "created"
        );
    }

    @Test
    public void testFillValueException() throws SqlException {
        execute("create table telem (created timestamp_ns, event_type int, table_id int, latency double) timestamp(created) partition by DAY");

        execute("insert into telem " +
                "select " +
                "generate_series as created," +
                " rnd_int() % 4," +
                " rnd_int() % 100," +
                " abs(rnd_double())" +
                " from generate_series('2025-01-20T13:57:14.000000000Z', dateadd('u', 2500 * 100_000, '2025-01-20T13:57:14.000000000Z') - 1, 2500000)");

        assertQueryNoLeakCheck(
                """
                        created\tlatency
                        2025-01-20T13:56:50.000000000Z\t0.0
                        2025-01-20T13:56:52.000000000Z\t0.0
                        2025-01-20T13:56:54.000000000Z\t0.0
                        2025-01-20T13:56:56.000000000Z\t0.0
                        2025-01-20T13:56:58.000000000Z\t0.0
                        2025-01-20T13:57:00.000000000Z\t0.0
                        2025-01-20T13:57:02.000000000Z\t0.0
                        2025-01-20T13:57:04.000000000Z\t0.0
                        2025-01-20T13:57:06.000000000Z\t0.0
                        2025-01-20T13:57:08.000000000Z\t0.0
                        2025-01-20T13:57:10.000000000Z\t0.0
                        2025-01-20T13:57:12.000000000Z\t0.0
                        2025-01-20T13:57:14.000000000Z\t0.8745107650756836
                        2025-01-20T13:57:16.000000000Z\t0.8901357650756836
                        2025-01-20T13:57:18.000000000Z\t0.8872051239013672
                        2025-01-20T13:57:20.000000000Z\t0.8901366591453552
                        2025-01-20T13:57:22.000000000Z\t0.9111318588256836
                        2025-01-20T13:57:24.000000000Z\t0.9057612419128418
                        2025-01-20T13:57:26.000000000Z\t0.9077143669128418
                        2025-01-20T13:57:28.000000000Z\t0.8959951400756836
                        2025-01-20T13:57:30.000000000Z\t0.9023432731628418
                        2025-01-20T13:57:32.000000000Z\t0.8984373807907104
                        2025-01-20T13:57:34.000000000Z\t0.9057607650756836
                        2025-01-20T13:57:36.000000000Z\t0.8852519989013672
                        2025-01-20T13:57:38.000000000Z\t0.9028315544128418
                        2025-01-20T13:57:40.000000000Z\t0.8979473114013672
                        2025-01-20T13:57:42.000000000Z\t0.9116191864013672
                        2025-01-20T13:57:44.000000000Z\t0.9096670150756836
                        2025-01-20T13:57:46.000000000Z\t0.9145498275756836
                        2025-01-20T13:57:48.000000000Z\t0.8930625915527344
                        2025-01-20T13:57:50.000000000Z\t0.8911123275756836
                        2025-01-20T13:57:52.000000000Z\t0.8969717025756836
                        2025-01-20T13:57:54.000000000Z\t0.8632811903953552
                        2025-01-20T13:57:56.000000000Z\t0.9052729606628418
                        2025-01-20T13:57:58.000000000Z\t0.9042966365814209
                        2025-01-20T13:58:00.000000000Z\t0.8984370231628418
                        2025-01-20T13:58:02.000000000Z\t0.9038066864013672
                        2025-01-20T13:58:04.000000000Z\t0.8984372615814209
                        2025-01-20T13:58:06.000000000Z\t0.8872067928314209
                        2025-01-20T13:58:08.000000000Z\t0.9145488739013672
                        2025-01-20T13:58:10.000000000Z\t0.8940410614013672
                        2025-01-20T13:58:12.000000000Z\t0.9042949676513672
                        2025-01-20T13:58:14.000000000Z\t0.9023427963256836
                        2025-01-20T13:58:16.000000000Z\t0.9086909294128418
                        2025-01-20T13:58:18.000000000Z\t0.9023432731628418
                        2025-01-20T13:58:20.000000000Z\t0.9062480926513672
                        2025-01-20T13:58:22.000000000Z\t0.9082030057907104
                        2025-01-20T13:58:24.000000000Z\t0.8999004364013672
                        2025-01-20T13:58:26.000000000Z\t0.8969721794128418
                        2025-01-20T13:58:28.000000000Z\t0.8984355926513672
                        2025-01-20T13:58:30.000000000Z\t0.9135732650756836
                        2025-01-20T13:58:32.000000000Z\t0.9082026481628418
                        2025-01-20T13:58:34.000000000Z\t0.8989248275756836
                        2025-01-20T13:58:36.000000000Z\t0.8916015475988388
                        2025-01-20T13:58:38.000000000Z\t0.8940420150756836
                        2025-01-20T13:58:40.000000000Z\t0.8886718747671694
                        2025-01-20T13:58:42.000000000Z\t0.8945307731628418
                        2025-01-20T13:58:44.000000000Z\t0.9101561903953552
                        2025-01-20T13:58:46.000000000Z\t0.8999013900756836
                        2025-01-20T13:58:48.000000000Z\t0.8935545682907104
                        2025-01-20T13:58:50.000000000Z\t0.9042967557907104
                        2025-01-20T13:58:52.000000000Z\t0.9033198356628418
                        2025-01-20T13:58:54.000000000Z\t0.9067382216453552
                        2025-01-20T13:58:56.000000000Z\t0.8867177963256836
                        2025-01-20T13:58:58.000000000Z\t0.9077129364013672
                        2025-01-20T13:59:00.000000000Z\t0.9052729606628418
                        2025-01-20T13:59:02.000000000Z\t0.8964824676513672
                        2025-01-20T13:59:04.000000000Z\t0.8920896053314209
                        2025-01-20T13:59:06.000000000Z\t0.9077138900756836
                        2025-01-20T13:59:08.000000000Z\t0.9096674919128418
                        2025-01-20T13:59:10.000000000Z\t0.8901362419128418
                        2025-01-20T13:59:12.000000000Z\t0.9067380428314209
                        2025-01-20T13:59:14.000000000Z\t0.9086894989013672
                        2025-01-20T13:59:16.000000000Z\t0.8930644989013672
                        2025-01-20T13:59:18.000000000Z\t0.9077110290527344
                        2025-01-20T13:59:20.000000000Z\t0.8979487419128418
                        2025-01-20T13:59:22.000000000Z\t0.9350583553314209
                        2025-01-20T13:59:24.000000000Z\t0.9091792106628418
                        2025-01-20T13:59:26.000000000Z\t0.8920893669128418
                        2025-01-20T13:59:28.000000000Z\t0.8867182731628418
                        2025-01-20T13:59:30.000000000Z\t0.8950190544128418
                        2025-01-20T13:59:32.000000000Z\t0.8994121551513672
                        2025-01-20T13:59:34.000000000Z\t0.9130858182907104
                        2025-01-20T13:59:36.000000000Z\t0.9013652801513672
                        2025-01-20T13:59:38.000000000Z\t0.9033198356628418
                        2025-01-20T13:59:40.000000000Z\t0.8998985290527344
                        2025-01-20T13:59:42.000000000Z\t0.8920897841453552
                        2025-01-20T13:59:44.000000000Z\t0.8989255428314209
                        2025-01-20T13:59:46.000000000Z\t0.8959956169128418
                        2025-01-20T13:59:48.000000000Z\t0.8784179091453552
                        2025-01-20T13:59:50.000000000Z\t0.8984355926513672
                        2025-01-20T13:59:52.000000000Z\t0.9086904525756836
                        2025-01-20T13:59:54.000000000Z\t0.9018516540527344
                        2025-01-20T13:59:56.000000000Z\t0.9023418426513672
                        2025-01-20T13:59:58.000000000Z\t0.8989248275756836
                        2025-01-20T14:00:00.000000000Z\t0.8847651481628418
                        2025-01-20T14:00:02.000000000Z\t0.8945293426513672
                        2025-01-20T14:00:04.000000000Z\t0.9111326932907104
                        2025-01-20T14:00:06.000000000Z\t0.9052729606628418
                        2025-01-20T14:00:08.000000000Z\t0.9160155057907104
                        2025-01-20T14:00:10.000000000Z\t0.8911113739013672
                        2025-01-20T14:00:12.000000000Z\t0.8881834745407104
                        2025-01-20T14:00:14.000000000Z\t0.8852519989013672
                        2025-01-20T14:00:16.000000000Z\t0.9125975966453552
                        2025-01-20T14:00:18.000000000Z\t0.8911123275756836
                        2025-01-20T14:00:20.000000000Z\t0.8911113739013672
                        2025-01-20T14:00:22.000000000Z\t0.8754844665527344
                        2025-01-20T14:00:24.000000000Z\t0.9018553495407104
                        2025-01-20T14:00:26.000000000Z\t0.8959941864013672
                        2025-01-20T14:00:28.000000000Z\t0.8920879364013672
                        2025-01-20T14:00:30.000000000Z\t0.8730449676513672
                        2025-01-20T14:00:32.000000000Z\t0.9077148139476776
                        2025-01-20T14:00:34.000000000Z\t0.8945311307907104
                        2025-01-20T14:00:36.000000000Z\t0.9057614803314209
                        2025-01-20T14:00:38.000000000Z\t0.9125971794128418
                        2025-01-20T14:00:40.000000000Z\t0.9091795682907104
                        2025-01-20T14:00:42.000000000Z\t0.8955075740814209
                        2025-01-20T14:00:44.000000000Z\t0.8847655057907104
                        2025-01-20T14:00:46.000000000Z\t0.9169912338256836
                        2025-01-20T14:00:48.000000000Z\t0.8974606990814209
                        2025-01-20T14:00:50.000000000Z\t0.9023432731628418
                        2025-01-20T14:00:52.000000000Z\t0.9160155057907104
                        2025-01-20T14:00:54.000000000Z\t0.9038081169128418
                        2025-01-20T14:00:56.000000000Z\t0.8588864803314209
                        2025-01-20T14:00:58.000000000Z\t0.8999018669128418
                        2025-01-20T14:01:00.000000000Z\t0.9042959213256836
                        2025-01-20T14:01:02.000000000Z\t0.8916015475988388
                        2025-01-20T14:01:04.000000000Z\t0.8964838981628418
                        2025-01-20T14:01:06.000000000Z\t0.9028317928314209
                        2025-01-20T14:01:08.000000000Z\t0.8867182731628418
                        2025-01-20T14:01:10.000000000Z\t0.8945307731628418
                        2025-01-20T14:01:12.000000000Z\t0.8979489803314209
                        2025-01-20T14:01:14.000000000Z\t0.8964842557907104
                        2025-01-20T14:01:16.000000000Z\t0.8916010856628418
                        2025-01-20T14:01:18.000000000Z\t0.9023418426513672
                        2025-01-20T14:01:20.000000000Z\t0.9038083553314209
                        2025-01-20T14:01:22.000000000Z\t0.8930654525756836
                        2025-01-20T14:01:24.000000000Z\t0.0
                        2025-01-20T14:01:26.000000000Z\t0.0
                        2025-01-20T14:01:28.000000000Z\t0.0
                        2025-01-20T14:01:30.000000000Z\t0.0
                        2025-01-20T14:01:32.000000000Z\t0.0
                        2025-01-20T14:01:34.000000000Z\t0.0
                        2025-01-20T14:01:36.000000000Z\t0.0
                        2025-01-20T14:01:38.000000000Z\t0.0
                        2025-01-20T14:01:40.000000000Z\t0.0
                        2025-01-20T14:01:42.000000000Z\t0.0
                        2025-01-20T14:01:44.000000000Z\t0.0
                        2025-01-20T14:01:46.000000000Z\t0.0
                        2025-01-20T14:01:48.000000000Z\t0.0
                        2025-01-20T14:01:50.000000000Z\t0.0
                        """,
                """
                        select created, approx_percentile(latency, 0.9, 3) latency
                          from telem
                          sample by 2s
                          FROM timestamp_floor('2s', '2025-01-20T13:56:50Z')
                          TO timestamp_floor('2s', '2025-01-20T14:01:52Z')
                          fill(0);""",
                "created"
        );
    }

    @Test
    public void testGeohashFillNull() throws Exception {
        assertQuery(
                """
                        s\tk\tfirst\tfirst1\tfirst2\tfirst3
                        TJW\t1970-01-03T00:00:00.000000000Z\t010\tc93\tfu3r7c\t5ewm40wx
                        PSWH\t1970-01-03T00:00:00.000000000Z\t\t\t\t
                        TJW\t1970-01-03T00:30:00.000000000Z\t\t\t\t
                        PSWH\t1970-01-03T00:30:00.000000000Z\t\t\t\t
                        TJW\t1970-01-03T01:00:00.000000000Z\t\t\t\t
                        PSWH\t1970-01-03T01:00:00.000000000Z\t110\ttk5\txn8nmw\t0n2gm6r7
                        """,
                "select s, k, " +
                        "first(g1), " +
                        "first(g2), " +
                        "first(g4), " +
                        "first(g8) " +
                        "from x sample by 30m fill(NULL)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_geohash(3) g1," +
                        " rnd_geohash(15) g2," +
                        " rnd_geohash(30) g4," +
                        " rnd_geohash(40) g8," +
                        " rnd_symbol(2,3,4,0) s, " +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(2)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testGeohashFillPrev() throws Exception {
        assertQuery(
                """
                        s\tk\tfirst\tfirst1\tfirst2\tfirst3
                        TJW\t1970-01-03T00:00:00.000000000Z\t010\tc93\tfu3r7c\t5ewm40wx
                        PSWH\t1970-01-03T00:00:00.000000000Z\t\t\t\t
                        TJW\t1970-01-03T00:30:00.000000000Z\t010\tc93\tfu3r7c\t5ewm40wx
                        PSWH\t1970-01-03T00:30:00.000000000Z\t\t\t\t
                        TJW\t1970-01-03T01:00:00.000000000Z\t010\tc93\tfu3r7c\t5ewm40wx
                        PSWH\t1970-01-03T01:00:00.000000000Z\t110\ttk5\txn8nmw\t0n2gm6r7
                        """,
                "select s, k, " +
                        "first(g1), " +
                        "first(g2), " +
                        "first(g4), " +
                        "first(g8) " +
                        "from x sample by 30m fill(PREV)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_geohash(3) g1," +
                        " rnd_geohash(15) g2," +
                        " rnd_geohash(30) g4," +
                        " rnd_geohash(40) g8," +
                        " rnd_symbol(2,3,4,0) s, " +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(2)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testGeohashInterpolated() throws Exception {
        assertException(
                "select k, first(b) from x sample by 3h fill(linear)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_geohash(30) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                10,
                "Unsupported interpolation type: GEOHASH(6c)"
        );
    }

    @Test
    public void testGroupByAllTypesAndInvalidTimestampColumn() throws Exception {
        assertException(
                """
                        select\s
                            LastUpdate,\s
                            CountryRegion,\s
                            last(Confirmed) Confirmed,\s
                            last(Recovered) Recovered,\s
                            last(Deaths) Deaths\s
                            from (
                                select\s
                                    LastUpdate,\s
                                    CountryRegion,\s
                                    sum(Confirmed) Confirmed,\s
                                    sum(Recovered) Recovered,\s
                                    sum(Deaths) Deaths
                                from (
                                    select\s
                                        LastUpdate,\s
                                        ProvinceState,\s
                                        CountryRegion,\s
                                        last(Confirmed) Confirmed,\s
                                        last(Recovered) Recovered,\s
                                        last(Deaths) Deaths
                                    from (covid where CountryRegion in ('China', 'Mainland China'))
                                    sample by 1d fill(prev)
                                ) timestamp(xy)
                            ) sample by 1M
                        ;
                        """,
                "create table covid as " +
                        "(" +
                        "select" +
                        " rnd_symbol(5,4,4,1) ProvinceState," +
                        " rnd_symbol(5,4,4,1) CountryRegion," +
                        " abs(rnd_int()) Confirmed," +
                        " abs(rnd_int()) Recovered," +
                        " abs(rnd_int()) Deaths," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) LastUpdate" +
                        " from" +
                        " long_sequence(1000)" +
                        ") timestamp(LastUpdate) partition by NONE",
                707,
                "Invalid column: xy"
        );
    }

    @Test
    public void testGroupByAllTypesAndInvalidTimestampType() throws Exception {
        assertException(
                """
                        select\s
                            LastUpdate,\s
                            CountryRegion,\s
                            last(Confirmed) Confirmed,\s
                            last(Recovered) Recovered,\s
                            last(Deaths) Deaths\s
                            from (
                                select\s
                                    LastUpdate,\s
                                    CountryRegion,\s
                                    sum(Confirmed) Confirmed,\s
                                    sum(Recovered) Recovered,\s
                                    sum(Deaths) Deaths
                                from (
                                    select\s
                                        LastUpdate,\s
                                        ProvinceState,\s
                                        CountryRegion,\s
                                        last(Confirmed) Confirmed,\s
                                        last(Recovered) Recovered,\s
                                        last(Deaths) Deaths
                                    from (covid where CountryRegion in ('China', 'Mainland China'))
                                    sample by 1d fill(prev)
                                ) timestamp(ProvinceState)
                            ) sample by 1M
                        ;
                        """,
                "create table covid as " +
                        "(" +
                        "select" +
                        " rnd_symbol(5,4,4,1) ProvinceState," +
                        " rnd_symbol(5,4,4,1) CountryRegion," +
                        " abs(rnd_int()) Confirmed," +
                        " abs(rnd_int()) Recovered," +
                        " abs(rnd_int()) Deaths," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) LastUpdate" +
                        " from" +
                        " long_sequence(1000)" +
                        ") timestamp(LastUpdate) partition by NONE",
                707,
                "not a TIMESTAMP"
        );
    }

    @Test
    public void testGroupByAllTypesAndTimestampSameLevel() throws Exception {
        assertQuery(
                """
                        k\tsum\tsum1\tsum2\tsum3\tsum4\tsum5
                        1970-01-03T00:00:00.000000000Z\t11.42798\t42.17768841969397\t426455968\t42\t4924\t4086802474270249591
                        1970-01-03T01:00:00.000000000Z\t42.243565\t70.94360487171201\t1631244228\t50\t10900\t8349358446893356086
                        1970-01-03T02:00:00.000000000Z\t33.608253\t76.75673070796104\t422941535\t27\t32312\t4442449726822927731
                        1970-01-03T03:00:00.000000000Z\t81.46808\t12.503042190293423\t2085282008\t9\t11472\t8955092533521658248
                        1970-01-03T04:00:00.000000000Z\t67.61935\t34.35685332942956\t2144581835\t6\t10942\t3152466304308949756
                        1970-01-03T05:00:00.000000000Z\t41.381645\t55.22494170511608\t667031149\t38\t22298\t5536695302686527374
                        1970-01-03T06:00:00.000000000Z\t97.50198\t0.11075361080621349\t1515787781\t49\t19013\t7316123607359392486
                        1970-01-03T07:00:00.000000000Z\t4.142809\t92.050039469858\t1299391311\t31\t19997\t4091897709796604687
                        1970-01-03T08:00:00.000000000Z\t22.822332\t88.37421918800908\t1269042121\t9\t6093\t4608960730952244094
                        1970-01-03T09:00:00.000000000Z\t72.300156\t12.105630273556178\t572338288\t28\t24397\t8081265393416742311
                        1970-01-03T10:00:00.000000000Z\t81.64182\t91.0141759290032\t1609750740\t3\t14377\t6161552193869048721
                        1970-01-03T11:00:00.000000000Z\t96.40289\t42.02044253932608\t712702244\t46\t22661\t2762535352290012031
                        1970-01-03T12:00:00.000000000Z\t67.52509\t95.40069089049732\t865832060\t48\t1315\t9063592617902736531
                        1970-01-03T13:00:00.000000000Z\t14.830547\t94.41658975532606\t2043803188\t6\t1464\t9144172287200792483
                        1970-01-03T14:00:00.000000000Z\t57.97447\t76.57837745299521\t462277692\t40\t21561\t9143800334706665900
                        1970-01-03T15:00:00.000000000Z\t39.017307\t10.643046345788132\t1238491107\t13\t30722\t6912707344119330199
                        1970-01-03T16:00:00.000000000Z\t48.927433\t82.31249461985348\t805434743\t31\t18600\t6187389706549636253
                        1970-01-03T17:00:00.000000000Z\t58.93398\t56.99444693578853\t1311366306\t9\t27078\t8755128364143858197
                        1970-01-03T18:00:00.000000000Z\t65.40475\t86.7718184863495\t593242882\t6\t23251\t5292387498953709416
                        1970-01-03T19:00:00.000000000Z\t85.93131\t33.74707565497281\t2105201404\t34\t14733\t8994301462266164776
                        """,
                "(select k, sum(a), sum(c), sum(d), sum(e), sum(f), sum(g) from x order by k) timestamp(k)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_float(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " rnd_double(0)*100 c," +
                        " abs(rnd_int()) d," +
                        " rnd_byte(2, 50) e," +
                        " abs(rnd_short()) f," +
                        " abs(rnd_long()) g," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_float(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " rnd_double(0)*100 c," +
                        " abs(rnd_int()) d," +
                        " rnd_byte(2, 50) e," +
                        " abs(rnd_short()) f," +
                        " abs(rnd_long()) g," +
                        " timestamp_sequence_ns(277200000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        k\tsum\tsum1\tsum2\tsum3\tsum4\tsum5
                        1970-01-03T00:00:00.000000000Z\t11.42798\t42.17768841969397\t426455968\t42\t4924\t4086802474270249591
                        1970-01-03T01:00:00.000000000Z\t42.243565\t70.94360487171201\t1631244228\t50\t10900\t8349358446893356086
                        1970-01-03T02:00:00.000000000Z\t33.608253\t76.75673070796104\t422941535\t27\t32312\t4442449726822927731
                        1970-01-03T03:00:00.000000000Z\t81.46808\t12.503042190293423\t2085282008\t9\t11472\t8955092533521658248
                        1970-01-03T04:00:00.000000000Z\t67.61935\t34.35685332942956\t2144581835\t6\t10942\t3152466304308949756
                        1970-01-03T05:00:00.000000000Z\t41.381645\t55.22494170511608\t667031149\t38\t22298\t5536695302686527374
                        1970-01-03T06:00:00.000000000Z\t97.50198\t0.11075361080621349\t1515787781\t49\t19013\t7316123607359392486
                        1970-01-03T07:00:00.000000000Z\t4.142809\t92.050039469858\t1299391311\t31\t19997\t4091897709796604687
                        1970-01-03T08:00:00.000000000Z\t22.822332\t88.37421918800908\t1269042121\t9\t6093\t4608960730952244094
                        1970-01-03T09:00:00.000000000Z\t72.300156\t12.105630273556178\t572338288\t28\t24397\t8081265393416742311
                        1970-01-03T10:00:00.000000000Z\t81.64182\t91.0141759290032\t1609750740\t3\t14377\t6161552193869048721
                        1970-01-03T11:00:00.000000000Z\t96.40289\t42.02044253932608\t712702244\t46\t22661\t2762535352290012031
                        1970-01-03T12:00:00.000000000Z\t67.52509\t95.40069089049732\t865832060\t48\t1315\t9063592617902736531
                        1970-01-03T13:00:00.000000000Z\t14.830547\t94.41658975532606\t2043803188\t6\t1464\t9144172287200792483
                        1970-01-03T14:00:00.000000000Z\t57.97447\t76.57837745299521\t462277692\t40\t21561\t9143800334706665900
                        1970-01-03T15:00:00.000000000Z\t39.017307\t10.643046345788132\t1238491107\t13\t30722\t6912707344119330199
                        1970-01-03T16:00:00.000000000Z\t48.927433\t82.31249461985348\t805434743\t31\t18600\t6187389706549636253
                        1970-01-03T17:00:00.000000000Z\t58.93398\t56.99444693578853\t1311366306\t9\t27078\t8755128364143858197
                        1970-01-03T18:00:00.000000000Z\t65.40475\t86.7718184863495\t593242882\t6\t23251\t5292387498953709416
                        1970-01-03T19:00:00.000000000Z\t85.93131\t33.74707565497281\t2105201404\t34\t14733\t8994301462266164776
                        1970-01-04T05:00:00.000000000Z\t98.5907\t98.8401109488745\t1912061086\t30\t17824\t8857660828600848720
                        1970-01-04T06:00:00.000000000Z\t50.258904\t38.42254384471547\t597366062\t21\t23702\t7037372650941669660
                        1970-01-04T07:00:00.000000000Z\t76.681465\t5.158459929273784\t1920398380\t38\t16628\t3527911398466283309
                        1970-01-04T08:00:00.000000000Z\t4.36064\t35.68111021227658\t503883303\t38\t10895\t7202923278768687325
                        1970-01-04T09:00:00.000000000Z\t45.920677\t76.06252634124596\t2043541236\t21\t19278\t1832315370633201942
                        """,
                true,
                true,
                false
        );
    }

    @Test
    public void testIndexSampleBy() throws Exception {
        assertQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-04T00:26:40.000000000Z\ta\t70.00560222114518\t168.04971262491318
                        1970-01-04T01:26:40.000000000Z\ta\t6.612327943200507\t151.3046788842135
                        1970-01-04T02:26:40.000000000Z\ta\t117.11888283070247\tnull
                        1970-01-04T03:26:40.000000000Z\ta\t99.02039650915859\t128.42101395467057
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from x " +
                        "where k > '1970-01-04' and s in ('a') " +
                        "sample by 1h align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns(172800000000000, 1000000000000) k" +
                        "   from" +
                        "   long_sequence(100)" +
                        "), index(s capacity 10) timestamp(k) partition by DAY",
                "k",
                false
        );
        assertQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-04T00:00:00.000000000Z\ta\t70.00560222114518\t168.04971262491318
                        1970-01-04T01:00:00.000000000Z\ta\t6.612327943200507\t151.3046788842135
                        1970-01-04T03:00:00.000000000Z\ta\t117.11888283070247\t128.42101395467057
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from x " +
                        "where k > '1970-01-04' and s in ('a') " +
                        "sample by 1h",
                "k",
                true,
                true
        );
        assertQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-04T00:00:00.000000000Z\ta\t70.00560222114518\t168.04971262491318
                        1970-01-04T01:00:00.000000000Z\ta\t6.612327943200507\t151.3046788842135
                        1970-01-04T03:00:00.000000000Z\ta\t117.11888283070247\t128.42101395467057
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from x " +
                        "where k > '1970-01-04' and s in ('a') " +
                        "sample by 1h align to calendar",
                "k",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleBy2a() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 256) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to calendar",
                "k",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleBy2b() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 256) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T00:20:00.000000000Z\tb\t-3.0\t7.0
                        1970-01-01T01:20:00.000000000Z\tb\t-9.0\t13.0
                        1970-01-01T02:20:00.000000000Z\tb\t-15.0\t19.0
                        1970-01-01T03:20:00.000000000Z\tb\t-21.0\t25.0
                        1970-01-01T04:20:00.000000000Z\tb\t-27.0\t31.0
                        1970-01-01T05:20:00.000000000Z\tb\t-33.0\t37.0
                        1970-01-01T06:20:00.000000000Z\tb\t-39.0\t43.0
                        1970-01-01T07:20:00.000000000Z\tb\t-45.0\t49.0
                        1970-01-01T08:20:00.000000000Z\tb\t-51.0\t55.0
                        1970-01-01T09:20:00.000000000Z\tb\t-57.0\t61.0
                        1970-01-01T10:20:00.000000000Z\tb\t-63.0\t67.0
                        1970-01-01T11:20:00.000000000Z\tb\t-69.0\t73.0
                        1970-01-01T12:20:00.000000000Z\tb\t-75.0\t79.0
                        1970-01-01T13:20:00.000000000Z\tb\t-81.0\t85.0
                        1970-01-01T14:20:00.000000000Z\tb\t-87.0\t91.0
                        1970-01-01T15:20:00.000000000Z\tb\t-93.0\t97.0
                        1970-01-01T16:20:00.000000000Z\tb\t-99.0\t103.0
                        1970-01-01T17:20:00.000000000Z\tb\t-105.0\t109.0
                        1970-01-01T18:20:00.000000000Z\tb\t-111.0\t115.0
                        1970-01-01T19:20:00.000000000Z\tb\t-117.0\t121.0
                        1970-01-01T20:20:00.000000000Z\tb\t-123.0\t127.0
                        1970-01-01T21:20:00.000000000Z\tb\t-129.0\t133.0
                        1970-01-01T22:20:00.000000000Z\tb\t-135.0\t139.0
                        1970-01-01T23:20:00.000000000Z\tb\t-141.0\t145.0
                        1970-01-02T00:20:00.000000000Z\tb\t-147.0\t149.0
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k > '1970-01-01' and s in ('b')" +
                        "sample by 1h align to first observation",
                """
                        insert into xx \
                        select -x lat,
                        x lon,
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(0, 10 * 60 * 1000000L) k
                        from
                        long_sequence(150)
                        """
        );
    }

    @Test
    public void testIndexSampleBy2c() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 256) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T00:00:00.000000000Z\tb\t-3.0\t5.0
                        1970-01-01T01:00:00.000000000Z\tb\t-7.0\t11.0
                        1970-01-01T02:00:00.000000000Z\tb\t-13.0\t17.0
                        1970-01-01T03:00:00.000000000Z\tb\t-19.0\t23.0
                        1970-01-01T04:00:00.000000000Z\tb\t-25.0\t29.0
                        1970-01-01T05:00:00.000000000Z\tb\t-31.0\t35.0
                        1970-01-01T06:00:00.000000000Z\tb\t-37.0\t41.0
                        1970-01-01T07:00:00.000000000Z\tb\t-43.0\t47.0
                        1970-01-01T08:00:00.000000000Z\tb\t-49.0\t53.0
                        1970-01-01T09:00:00.000000000Z\tb\t-55.0\t59.0
                        1970-01-01T10:00:00.000000000Z\tb\t-61.0\t65.0
                        1970-01-01T11:00:00.000000000Z\tb\t-67.0\t71.0
                        1970-01-01T12:00:00.000000000Z\tb\t-73.0\t77.0
                        1970-01-01T13:00:00.000000000Z\tb\t-79.0\t83.0
                        1970-01-01T14:00:00.000000000Z\tb\t-85.0\t89.0
                        1970-01-01T15:00:00.000000000Z\tb\t-91.0\t95.0
                        1970-01-01T16:00:00.000000000Z\tb\t-97.0\t101.0
                        1970-01-01T17:00:00.000000000Z\tb\t-103.0\t107.0
                        1970-01-01T18:00:00.000000000Z\tb\t-109.0\t113.0
                        1970-01-01T19:00:00.000000000Z\tb\t-115.0\t119.0
                        1970-01-01T20:00:00.000000000Z\tb\t-121.0\t125.0
                        1970-01-01T21:00:00.000000000Z\tb\t-127.0\t131.0
                        1970-01-01T22:00:00.000000000Z\tb\t-133.0\t137.0
                        1970-01-01T23:00:00.000000000Z\tb\t-139.0\t143.0
                        1970-01-02T00:00:00.000000000Z\tb\t-145.0\t149.0
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k > '1970-01-01' and s in ('b')" +
                        "sample by 1h align to calendar",
                """
                        insert into xx \
                        select -x lat,
                        x lon,
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(0, 10 * 60 * 1000000L) k
                        from
                        long_sequence(150)
                        """,
                true,
                true
        );
    }

    @Test
    public void testIndexSampleBy2d() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 256) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T00:20:00.000000000Z\tb\t-3.0\t7.0
                        1970-01-01T01:20:00.000000000Z\tb\t-9.0\t13.0
                        1970-01-01T02:20:00.000000000Z\tb\t-15.0\t19.0
                        1970-01-01T03:20:00.000000000Z\tb\t-21.0\t25.0
                        1970-01-01T04:20:00.000000000Z\tb\t-27.0\t31.0
                        1970-01-01T05:20:00.000000000Z\tb\t-33.0\t37.0
                        1970-01-01T06:20:00.000000000Z\tb\t-39.0\t43.0
                        1970-01-01T07:20:00.000000000Z\tb\t-45.0\t49.0
                        1970-01-01T08:20:00.000000000Z\tb\t-51.0\t55.0
                        1970-01-01T09:20:00.000000000Z\tb\t-57.0\t61.0
                        1970-01-01T10:20:00.000000000Z\tb\t-63.0\t67.0
                        1970-01-01T11:20:00.000000000Z\tb\t-69.0\t73.0
                        1970-01-01T12:20:00.000000000Z\tb\t-75.0\t79.0
                        1970-01-01T13:20:00.000000000Z\tb\t-81.0\t85.0
                        1970-01-01T14:20:00.000000000Z\tb\t-87.0\t91.0
                        1970-01-01T15:20:00.000000000Z\tb\t-93.0\t97.0
                        1970-01-01T16:20:00.000000000Z\tb\t-99.0\t103.0
                        1970-01-01T17:20:00.000000000Z\tb\t-105.0\t109.0
                        1970-01-01T18:20:00.000000000Z\tb\t-111.0\t115.0
                        1970-01-01T19:20:00.000000000Z\tb\t-117.0\t121.0
                        1970-01-01T20:20:00.000000000Z\tb\t-123.0\t127.0
                        1970-01-01T21:20:00.000000000Z\tb\t-129.0\t133.0
                        1970-01-01T22:20:00.000000000Z\tb\t-135.0\t139.0
                        1970-01-01T23:20:00.000000000Z\tb\t-141.0\t145.0
                        1970-01-02T00:20:00.000000000Z\tb\t-147.0\t149.0
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k > '1970-01-01' and s in ('b')" +
                        "sample by 1h align to first observation",
                """
                        insert into xx \
                        select -x lat,
                        x lon,
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(0, 10 * 60 * 1000000L) k
                        from
                        long_sequence(150)
                        """
        );

        assertWithSymbolColumnTop(
                """
                        k\ts\tlat\tlon
                        1970-01-01T00:10:00.000000000Z\t\t-2.0\t13.0
                        1970-01-01T02:10:00.000000000Z\t\t-14.0\t25.0
                        1970-01-01T04:10:00.000000000Z\t\t-26.0\t37.0
                        1970-01-01T06:10:00.000000000Z\t\t-38.0\t49.0
                        1970-01-01T08:10:00.000000000Z\t\t-50.0\t61.0
                        1970-01-01T10:10:00.000000000Z\t\t-62.0\t73.0
                        1970-01-01T12:10:00.000000000Z\t\t-74.0\t85.0
                        1970-01-01T14:10:00.000000000Z\t\t-86.0\t97.0
                        1970-01-01T16:10:00.000000000Z\t\t-98.0\t109.0
                        1970-01-01T18:10:00.000000000Z\t\t-110.0\t121.0
                        1970-01-01T20:10:00.000000000Z\t\t-122.0\t133.0
                        1970-01-01T22:10:00.000000000Z\t\t-134.0\t145.0
                        1970-01-02T00:10:00.000000000Z\t\t-146.0\t150.0
                        """,
                """
                        select k, s, first(lat) lat, last(lon) lon\s
                        from xx\s
                        where k > '1970-01-01' and s = null\s
                        sample by 2h align to first observation"""
        );
    }

    @Test
    public void testIndexSampleBy2e() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to calendar",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 256) timestamp(k) partition by DAY",
                "k",
                true,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T00:00:00.000000000Z\tb\t-3.0\t5.0
                        1970-01-01T01:00:00.000000000Z\tb\t-7.0\t11.0
                        1970-01-01T02:00:00.000000000Z\tb\t-13.0\t17.0
                        1970-01-01T03:00:00.000000000Z\tb\t-19.0\t23.0
                        1970-01-01T04:00:00.000000000Z\tb\t-25.0\t29.0
                        1970-01-01T05:00:00.000000000Z\tb\t-31.0\t35.0
                        1970-01-01T06:00:00.000000000Z\tb\t-37.0\t41.0
                        1970-01-01T07:00:00.000000000Z\tb\t-43.0\t47.0
                        1970-01-01T08:00:00.000000000Z\tb\t-49.0\t53.0
                        1970-01-01T09:00:00.000000000Z\tb\t-55.0\t59.0
                        1970-01-01T10:00:00.000000000Z\tb\t-61.0\t65.0
                        1970-01-01T11:00:00.000000000Z\tb\t-67.0\t71.0
                        1970-01-01T12:00:00.000000000Z\tb\t-73.0\t77.0
                        1970-01-01T13:00:00.000000000Z\tb\t-79.0\t83.0
                        1970-01-01T14:00:00.000000000Z\tb\t-85.0\t89.0
                        1970-01-01T15:00:00.000000000Z\tb\t-91.0\t95.0
                        1970-01-01T16:00:00.000000000Z\tb\t-97.0\t101.0
                        1970-01-01T17:00:00.000000000Z\tb\t-103.0\t107.0
                        1970-01-01T18:00:00.000000000Z\tb\t-109.0\t113.0
                        1970-01-01T19:00:00.000000000Z\tb\t-115.0\t119.0
                        1970-01-01T20:00:00.000000000Z\tb\t-121.0\t125.0
                        1970-01-01T21:00:00.000000000Z\tb\t-127.0\t131.0
                        1970-01-01T22:00:00.000000000Z\tb\t-133.0\t137.0
                        1970-01-01T23:00:00.000000000Z\tb\t-139.0\t143.0
                        1970-01-02T00:00:00.000000000Z\tb\t-145.0\t149.0
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k > '1970-01-01' and s in ('b')" +
                        "sample by 1h align to calendar",
                """
                        insert into xx \
                        select -x lat,
                        x lon,
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(0, 10 * 60 * 1000000L) k
                        from
                        long_sequence(150)
                        """,
                true,
                true
        );

        assertWithSymbolColumnTop(
                """
                        k\ts\tlat\tlon
                        1970-01-01T00:00:00.000000000Z\t\t-2.0\t12.0
                        1970-01-01T02:00:00.000000000Z\t\t-13.0\t24.0
                        1970-01-01T04:00:00.000000000Z\t\t-25.0\t36.0
                        1970-01-01T06:00:00.000000000Z\t\t-37.0\t48.0
                        1970-01-01T08:00:00.000000000Z\t\t-49.0\t60.0
                        1970-01-01T10:00:00.000000000Z\t\t-61.0\t72.0
                        1970-01-01T12:00:00.000000000Z\t\t-73.0\t84.0
                        1970-01-01T14:00:00.000000000Z\t\t-85.0\t96.0
                        1970-01-01T16:00:00.000000000Z\t\t-97.0\t108.0
                        1970-01-01T18:00:00.000000000Z\t\t-109.0\t120.0
                        1970-01-01T20:00:00.000000000Z\t\t-121.0\t132.0
                        1970-01-01T22:00:00.000000000Z\t\t-133.0\t144.0
                        1970-01-02T00:00:00.000000000Z\t\t-145.0\t150.0
                        """,
                """
                        select k, s, first(lat) lat, last(lon) lon\s
                        from xx\s
                        where k > '1970-01-01' and s = null\s
                        sample by 2h align to calendar""",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleBy3a() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 256) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to calendar",
                "k",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleBy3b() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 256) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T21:10:00.000000000Z\ta\t-128.0\t128.0
                        1970-01-01T23:10:00.000000000Z\ta\t-140.0\t140.0
                        1970-01-02T01:10:00.000000000Z\ta\t-152.0\t152.0
                        1970-01-02T03:10:00.000000000Z\ta\t-164.0\t164.0
                        1970-01-02T05:10:00.000000000Z\ta\t-176.0\t176.0
                        """,
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k > '1970-01-01T21:00' and s in ('a')" +
                        "sample by 2h align to first observation",
                """
                        insert into xx \
                        select -x lat,
                        x lon,
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(0, 10 * 60 * 1000000L) k
                        from
                        long_sequence(180)
                        """
        );
    }

    @Test
    public void testIndexSampleBy3c() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 256) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T20:00:00.000000000Z\ta\t-128.0\t128.0
                        1970-01-01T22:00:00.000000000Z\ta\t-134.0\t134.0
                        1970-01-02T00:00:00.000000000Z\ta\t-146.0\t146.0
                        1970-01-02T02:00:00.000000000Z\ta\t-158.0\t158.0
                        1970-01-02T04:00:00.000000000Z\ta\t-170.0\t170.0
                        """,
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k > '1970-01-01T21:00' and s in ('a')" +
                        "sample by 2h align to calendar",
                """
                        insert into xx \
                        select -x lat,
                        x lon,
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(0, 10 * 60 * 1000000L) k
                        from
                        long_sequence(180)
                        """,
                true,
                true
        );
    }

    @Test
    public void testIndexSampleBy3d() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 256) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T21:10:00.000000000Z\ta\t-128.0\t128.0
                        1970-01-01T23:10:00.000000000Z\ta\t-140.0\t140.0
                        1970-01-02T01:10:00.000000000Z\ta\t-152.0\t152.0
                        1970-01-02T03:10:00.000000000Z\ta\t-164.0\t164.0
                        1970-01-02T05:10:00.000000000Z\ta\t-176.0\t176.0
                        """,
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k > '1970-01-01T21:00' and s in ('a')" +
                        "sample by 2h align to first observation",
                """
                        insert into xx \
                        select -x lat,
                        x lon,
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(0, 10 * 60 * 1000000L) k
                        from
                        long_sequence(180)
                        """,
                false,
                false
        );

        assertWithSymbolColumnTop(
                """
                        k\ts\tlat\tlon
                        1970-01-01T21:10:00.000000000Z\t\t-128.0\t128.0
                        1970-01-01T23:10:00.000000000Z\t\t-140.0\t140.0
                        1970-01-02T01:10:00.000000000Z\t\t-152.0\t152.0
                        1970-01-02T03:10:00.000000000Z\t\t-164.0\t164.0
                        1970-01-02T05:10:00.000000000Z\t\t-176.0\t176.0
                        """,
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k > '1970-01-01T21:00' and s = null " +
                        "sample by 2h align to first observation"
        );
    }

    @Test
    public void testIndexSampleBy3e() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to calendar",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 256) timestamp(k) partition by DAY",
                "k",
                true,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T20:00:00.000000000Z\ta\t-128.0\t128.0
                        1970-01-01T22:00:00.000000000Z\ta\t-134.0\t134.0
                        1970-01-02T00:00:00.000000000Z\ta\t-146.0\t146.0
                        1970-01-02T02:00:00.000000000Z\ta\t-158.0\t158.0
                        1970-01-02T04:00:00.000000000Z\ta\t-170.0\t170.0
                        """,
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k > '1970-01-01T21:00' and s in ('a')" +
                        "sample by 2h align to calendar",
                """
                        insert into xx \
                        select -x lat,
                        x lon,
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(0, 10 * 60 * 1000000L) k
                        from
                        long_sequence(180)
                        """,
                true,
                true
        );

        assertWithSymbolColumnTop(
                "k\ts\tlat\tlon\n" +
                        "1970-01-01T20:00:00.000000000Z\t\t-128.0\t128.0\n" + // ????
                        "1970-01-01T22:00:00.000000000Z\t\t-133.0\t133.0\n" +
                        "1970-01-02T00:00:00.000000000Z\t\t-145.0\t145.0\n" +
                        "1970-01-02T02:00:00.000000000Z\t\t-157.0\t157.0\n" +
                        "1970-01-02T04:00:00.000000000Z\t\t-169.0\t169.0\n",
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k > '1970-01-01T21:00' and s = null " +
                        "sample by 2h align to calendar",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendar() throws Exception {
        String query = "select k, s, first(lat) lat, last(lon) lon " +
                "from x " +
                "where s in ('a') " +
                "sample by 1h align to calendar";

        String forceNoIndexQuery = query.replace("in ('b')", "in ('b', 'none')")
                .replace("in ('a')", "in ('a', 'none')");

        String expected = """
                k\ts\tlat\tlon
                2021-03-27T23:00:00.000000000Z\ta\t142.30215575416736\t165.69007104574442
                2021-03-28T00:00:00.000000000Z\ta\t106.0418967098362\tnull
                2021-03-28T01:00:00.000000000Z\ta\t79.9245166429184\t168.04971262491318
                2021-03-28T02:00:00.000000000Z\ta\t6.612327943200507\t128.42101395467057
                """;

        assertQuery(
                expected,
                forceNoIndexQuery,
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns('2021-03-27T23:30:00.00000000Z', 100000000000) k" +
                        "   from" +
                        "   long_sequence(100)" +
                        "),index(s) timestamp(k) partition by DAY",
                "k",
                true,
                true
        );

        assertQuery(
                expected,
                query,
                null,
                "k",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarBindVariables() throws Exception {
        assertMemoryLeak(() -> {
            execute(
                    "create table x as " +
                            "(" +
                            "select" +
                            "   rnd_double(1)*180 lat," +
                            "   rnd_double(1)*180 lon," +
                            "   rnd_symbol('a') s," +
                            "   timestamp_sequence_ns('2021-03-28T00:59:00.000000000Z', 60*1000000000L) k" +
                            "   from" +
                            "   long_sequence(100)" +
                            "), index(s) timestamp(k) partition by DAY",
                    sqlExecutionContext
            );

            snapshotMemoryUsage();
            try (
                    RecordCursorFactory factory = select(
                            "select k, s, first(lat) lat, last(lon) lon " +
                                    "from x " +
                                    "where s in ('a') " +
                                    "sample by 1h align to calendar time zone $1 with offset $2"
                    )
            ) {
                String expectedMoscow = """
                        k\ts\tlat\tlon
                        2021-03-28T00:15:00.000000000Z\ta\t144.77803379943109\tnull
                        2021-03-28T01:15:00.000000000Z\ta\t31.267026583720984\tnull
                        2021-03-28T02:15:00.000000000Z\ta\t103.7167928478985\t128.42101395467057
                        """;

                String expectedPrague = """
                        k\ts\tlat\tlon
                        2021-03-28T00:10:00.000000000Z\ta\t144.77803379943109\tnull
                        2021-03-28T01:10:00.000000000Z\ta\t137.95662156473048\tnull
                        2021-03-28T02:10:00.000000000Z\ta\tnull\t128.42101395467057
                        """;

                sqlExecutionContext.getBindVariableService().setStr(0, "Europe/Moscow");
                sqlExecutionContext.getBindVariableService().setStr(1, "00:15");
                try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                    assertCursor(
                            expectedMoscow,
                            cursor,
                            factory.getMetadata(),
                            true
                    );
                }
                assertFactoryMemoryUsage();

                // invalid timezone
                sqlExecutionContext.getBindVariableService().setStr(0, "Oopsie");
                sqlExecutionContext.getBindVariableService().setStr(1, "00:15");
                try {
                    factory.getCursor(sqlExecutionContext);
                    Assert.fail();
                } catch (SqlException e) {
                    Assert.assertEquals(108, e.getPosition());
                    TestUtils.assertContains(e.getFlyweightMessage(), "invalid timezone: Oopsie");
                }
                assertFactoryMemoryUsage();

                sqlExecutionContext.getBindVariableService().setStr(0, "Europe/Prague");
                sqlExecutionContext.getBindVariableService().setStr(1, "uggs");
                try {
                    factory.getCursor(sqlExecutionContext);
                    Assert.fail();
                } catch (SqlException e) {
                    Assert.assertEquals(123, e.getPosition());
                    TestUtils.assertContains(e.getFlyweightMessage(), "invalid offset: uggs");
                }
                assertFactoryMemoryUsage();

                sqlExecutionContext.getBindVariableService().setStr(0, "Europe/Prague");
                sqlExecutionContext.getBindVariableService().setStr(1, "00:10");
                try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                    assertCursor(
                            expectedPrague,
                            cursor,
                            factory.getMetadata(),
                            true
                    );
                }
                assertFactoryMemoryUsage();
            }
        });
    }

    @Test
    public void testIndexSampleByAlignToCalendarBindVariablesWrongTypes() throws Exception {
        assertMemoryLeak(() -> {
            execute(
                    "create table x as " +
                            "(" +
                            "select" +
                            "   rnd_double(1)*180 lat," +
                            "   rnd_double(1)*180 lon," +
                            "   rnd_symbol('a') s," +
                            "   timestamp_sequence_ns('2021-03-28T00:59:00.00000000Z', 60*1000000000L) k" +
                            "   from" +
                            "   long_sequence(100)" +
                            "), index(s) timestamp(k) partition by DAY"
            );

            String sql = "select k, s, first(lat) lat, last(lon) lon from x sample by 1h align to calendar time zone $1 with offset $2";

            try (RecordCursorFactory factory = select(sql)) {
                sqlExecutionContext.getBindVariableService().setLong(0, 42);
                sqlExecutionContext.getBindVariableService().setStr(1, "00:15");
                try {
                    try (RecordCursor ignore = factory.getCursor(sqlExecutionContext)) {
                        Assert.fail();
                    }
                } catch (SqlException e) {
                    Assert.assertEquals(91, e.getPosition());
                    TestUtils.assertContains(e.getFlyweightMessage(), "invalid timezone: 42");
                }
            }

            sqlExecutionContext.getBindVariableService().clear();
            try (RecordCursorFactory factory = select(sql)) {
                sqlExecutionContext.getBindVariableService().setStr(0, "Europe/Prague");
                sqlExecutionContext.getBindVariableService().setLong(1, 42);
                try {
                    try (RecordCursor ignore = factory.getCursor(sqlExecutionContext)) {
                        Assert.fail();
                    }
                } catch (SqlException e) {
                    Assert.assertEquals(106, e.getPosition());
                    TestUtils.assertContains(e.getFlyweightMessage(), "invalid offset: 42");
                }
            }
        });
    }

    @Test
    public void testIndexSampleByAlignToCalendarDSTForwardEdge() throws Exception {
        assertQuery(
                """
                        k\ts\tlat\tlon
                        2021-03-28T01:00:00.000000000Z\ta\t144.77803379943109\t15.276535618609202
                        2021-03-28T03:00:00.000000000Z\ta\tnull\t127.43011035722469
                        2021-03-28T04:00:00.000000000Z\ta\t60.30746433578906\t128.42101395467057
                        """,
                "select to_timezone(k, 'Europe/Berlin') k, s, lat, lon from (select k, s, first(lat) lat, last(lon) lon " +
                        "from x " +
                        "where s in ('a') " +
                        "sample by 1h align to calendar time zone 'Europe/Berlin')",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a') s," +
                        "   timestamp_sequence_ns('2021-03-28T00:59:00.00000000Z', 60*1000000000L) k" +
                        "   from" +
                        "   long_sequence(100)" +
                        "), index(s) timestamp(k) partition by DAY",
                null,
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarDSTForwardEdge2() throws Exception {
        assertQuery(
                """
                        k\ts\tlat\tlon
                        2021-03-28T03:00:00.000000000Z\ta\t144.77803379943109\tnull
                        2021-03-28T04:00:00.000000000Z\ta\t98.27279585461298\t128.42101395467057
                        """,
                "select to_timezone(k, 'Europe/Berlin') k, s, lat, lon from (select k, s, first(lat) lat, last(lon) lon " +
                        "from x " +
                        "where s in ('a') " +
                        "sample by 1h align to calendar time zone 'Europe/Berlin')",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a') s," +
                        "   timestamp_sequence_ns('2021-03-28T01:00:00.00000000Z', 60*1000000000L) k" +
                        "   from" +
                        "   long_sequence(100)" +
                        "), index(s) timestamp(k) partition by DAY",
                null,
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarDSTForwardEdge3() throws Exception {
        assertQuery(
                """
                        k\ts\tlat\tlon
                        2021-03-28T03:00:00.000000000Z\ta\t144.77803379943109\t15.276535618609202
                        2021-03-28T04:00:00.000000000Z\ta\tnull\t127.43011035722469
                        2021-03-28T05:00:00.000000000Z\ta\t60.30746433578906\t128.42101395467057
                        """,
                "select to_timezone(k, 'Europe/Berlin') k, s, lat, lon from (select k, s, first(lat) lat, last(lon) lon " +
                        "from x " +
                        "where s in ('a') " +
                        "sample by 1h align to calendar time zone 'Europe/Berlin')",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a') s," +
                        "   timestamp_sequence_ns('2021-03-28T01:59:00.000000000Z', 60*1000000000L) k" +
                        "   from" +
                        "   long_sequence(100)" +
                        "), index(s) timestamp(k) partition by DAY",
                null,
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarDSTForwardLocalMidnight() throws Exception {
        assertQuery(
                """
                        k\ts\tlat\tlon
                        2021-03-27T23:00:00.000000000Z\ta\t142.30215575416736\t167.4566019970139
                        2021-03-28T00:00:00.000000000Z\ta\t33.45558404694713\t128.42101395467057
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from x " +
                        "where s in ('a') " +
                        "sample by 1h align to calendar time zone 'Europe/Berlin'",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns('2021-03-27T23:01:00.00000000Z', 60*1000000000L) k" +
                        "   from" +
                        "   long_sequence(100)" +
                        "), index(s) timestamp(k) partition by DAY",
                "k",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarWithTimezoneBerlinShiftBack() throws Exception {
        assertSampleByIndexQuery(
                """
                        to_timezone\tk\ts\tlat\tlon
                        2020-10-24T00:00:00.000000000Z\t2020-10-23T22:00:00.000000000Z\ta\t142.30215575416736\t2020-10-24T19:50:00.000000000Z
                        2020-10-25T00:00:00.000000000Z\t2020-10-24T22:00:00.000000000Z\ta\tnull\t2020-10-25T20:00:00.000000000Z
                        2020-10-26T00:00:00.000000000Z\t2020-10-25T23:00:00.000000000Z\ta\t33.45558404694713\t2020-10-26T21:50:00.000000000Z
                        2020-10-27T00:00:00.000000000Z\t2020-10-26T23:00:00.000000000Z\ta\t6.612327943200507\t2020-10-27T22:00:00.000000000Z
                        2020-10-28T00:00:00.000000000Z\t2020-10-27T23:00:00.000000000Z\ta\tnull\t2020-10-27T23:40:00.000000000Z
                        """,
                "select to_timezone(k, 'Europe/Berlin'), k, s, lat, lon from (" +
                        "select k, s, first(lat) lat, last(k) lon " +
                        "from x " +
                        "where s in ('a') " +
                        "sample by 1d align to calendar time zone 'Europe/Berlin'" +
                        ")",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns('2020-10-23T20:30:00.000000000Z', 50 * 60 * 1000000000L) k" +
                        "   from" +
                        "   long_sequence(120)" +
                        "),index(s) timestamp(k)",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarWithTimezoneBerlinShiftBackHourly() throws Exception {
        assertSampleByIndexQuery(
                """
                        to_timezone\tk\ts\tlat\tlon
                        2021-03-27T22:00:00.000000000Z\t2021-03-27T21:00:00.000000000Z\ta\t132.09083798490755\t2021-03-27T21:51:00.000000000Z
                        2021-03-27T23:00:00.000000000Z\t2021-03-27T22:00:00.000000000Z\ta\t77.68770182183965\t2021-03-27T22:56:00.000000000Z
                        2021-03-28T00:00:00.000000000Z\t2021-03-27T23:00:00.000000000Z\ta\tnull\t2021-03-27T23:48:00.000000000Z
                        2021-03-28T01:00:00.000000000Z\t2021-03-28T00:00:00.000000000Z\ta\t3.6703591550328163\t2021-03-28T00:27:00.000000000Z
                        2021-03-28T03:00:00.000000000Z\t2021-03-28T01:00:00.000000000Z\ta\t94.70222369149758\t2021-03-28T01:45:00.000000000Z
                        2021-03-28T04:00:00.000000000Z\t2021-03-28T02:00:00.000000000Z\ta\t109.23418649425325\t2021-03-28T02:37:00.000000000Z
                        2021-03-28T05:00:00.000000000Z\t2021-03-28T03:00:00.000000000Z\ta\t38.20430552091481\t2021-03-28T03:16:00.000000000Z
                        """,
                "select to_timezone(k, 'Europe/Berlin'), k, s, lat, lon from (" +
                        "select k, s, first(lat) lat, last(k) lon " +
                        "from x " +
                        "where s in ('a') and k between '2021-03-27 21:00' and  '2021-03-28 04:00'" +
                        "sample by 1h align to calendar time zone 'Europe/Berlin'" +
                        ")",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b') s," +
                        "   timestamp_sequence_ns('2021-03-26T20:30:00.000000000Z', 13 * 60 * 1000000000L) k" +
                        "   from" +
                        "   long_sequence(1000)" +
                        "),index(s) timestamp(k)",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarWithTimezoneBerlinShiftBackHourlyWithOffset() throws Exception {
        assertSampleByIndexQuery(
                """
                        to_timezone\tk\ts\tlat\tlon
                        2021-03-27T21:15:00.000000000Z\t2021-03-27T20:15:00.000000000Z\ta\t132.09083798490755\t2021-03-27T21:12:00.000000000Z
                        2021-03-27T22:15:00.000000000Z\t2021-03-27T21:15:00.000000000Z\ta\t179.5841357536068\t2021-03-27T21:51:00.000000000Z
                        2021-03-27T23:15:00.000000000Z\t2021-03-27T22:15:00.000000000Z\ta\t77.68770182183965\t2021-03-27T22:56:00.000000000Z
                        2021-03-28T00:15:00.000000000Z\t2021-03-27T23:15:00.000000000Z\ta\tnull\t2021-03-27T23:48:00.000000000Z
                        2021-03-28T01:15:00.000000000Z\t2021-03-28T00:15:00.000000000Z\ta\t3.6703591550328163\t2021-03-28T01:06:00.000000000Z
                        2021-03-28T03:15:00.000000000Z\t2021-03-28T01:15:00.000000000Z\ta\tnull\t2021-03-28T02:11:00.000000000Z
                        2021-03-28T04:15:00.000000000Z\t2021-03-28T02:15:00.000000000Z\ta\tnull\t2021-03-28T02:37:00.000000000Z
                        2021-03-28T05:15:00.000000000Z\t2021-03-28T03:15:00.000000000Z\ta\t38.20430552091481\t2021-03-28T03:16:00.000000000Z
                        """,
                "select to_timezone(k, 'Europe/Berlin'), k, s, lat, lon from (" +
                        "  select k, s, first(lat) lat, last(k) lon " +
                        "  from x " +
                        "  where s in ('a') and k between '2021-03-27 21:00' and  '2021-03-28 04:00' " +
                        "  sample by 1h align to calendar time zone 'Europe/Berlin' with offset '00:15' " +
                        ")",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b') s," +
                        "   timestamp_sequence_ns('2021-03-26T20:30:00.000000000Z', 13 * 60 * 1000000000L) k" +
                        "   from" +
                        "   long_sequence(1000)" +
                        "),index(s) timestamp(k)",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarWithTimezoneBerlinShiftForward() throws Exception {
        assertSampleByIndexQuery(
                """
                        to_timezone\tk\ts\tlat\tlon
                        2021-03-26T00:00:00.000000000Z\t2021-03-25T23:00:00.000000000Z\ta\t142.30215575416736\t2021-03-26T22:50:00.000000000Z
                        2021-03-27T00:00:00.000000000Z\t2021-03-26T23:00:00.000000000Z\ta\tnull\t2021-03-27T22:10:00.000000000Z
                        2021-03-28T00:00:00.000000000Z\t2021-03-27T23:00:00.000000000Z\ta\t109.94209864193589\t2021-03-28T20:40:00.000000000Z
                        2021-03-29T00:00:00.000000000Z\t2021-03-28T22:00:00.000000000Z\ta\t70.00560222114518\t2021-03-29T16:40:00.000000000Z
                        2021-03-30T00:00:00.000000000Z\t2021-03-29T22:00:00.000000000Z\ta\t13.290235514836048\t2021-03-30T02:40:00.000000000Z
                        """,
                "select to_timezone(k, 'Europe/Berlin'), k, s, lat, lon from (" +
                        "select k, s, first(lat) lat, last(k) lon " +
                        "from x " +
                        "where s in ('a') " +
                        "sample by 1d align to calendar time zone 'Europe/Berlin'" +
                        ")",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns('2021-03-25T23:30:00.000000000Z', 50 * 60 * 1000000000L) k" +
                        "   from" +
                        "   long_sequence(120)" +
                        "),index(s) timestamp(k)",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarWithTimezoneLondon365Days() throws Exception {
        assertSampleByIndexQuery(
                """
                        to_timezone\tk\ts\tlat\tlon
                        2020-01-02T00:00:00.000000000Z\t2020-01-02T00:00:00.000000000Z\ta\t142.30215575416736\t2020-01-02T03:23:00.000000000Z
                        2020-02-15T00:00:00.000000000Z\t2020-02-15T00:00:00.000000000Z\ta\t135.8378128727785\t2020-02-15T17:44:30.000000000Z
                        2020-02-16T00:00:00.000000000Z\t2020-02-16T00:00:00.000000000Z\ta\t149.54213547651923\t2020-02-16T17:50:00.000000000Z
                        2020-03-31T00:00:00.000000000Z\t2020-03-30T23:00:00.000000000Z\ta\tnull\t2020-03-31T21:52:00.000000000Z
                        2020-04-01T00:00:00.000000000Z\t2020-03-31T23:00:00.000000000Z\ta\t136.72774150518887\t2020-04-01T04:45:00.000000000Z
                        2020-05-15T00:00:00.000000000Z\t2020-05-14T23:00:00.000000000Z\ta\t7.639734467015391\t2020-05-15T22:33:00.000000000Z
                        2020-05-16T00:00:00.000000000Z\t2020-05-15T23:00:00.000000000Z\ta\t140.2722640805293\t2020-05-16T22:38:30.000000000Z
                        2020-06-29T00:00:00.000000000Z\t2020-06-28T23:00:00.000000000Z\ta\tnull\t2020-06-29T19:47:30.000000000Z
                        2020-06-30T00:00:00.000000000Z\t2020-06-29T23:00:00.000000000Z\ta\t176.93009129230163\t2020-06-30T16:26:30.000000000Z
                        2020-09-27T00:00:00.000000000Z\t2020-09-26T23:00:00.000000000Z\ta\tnull\t2020-09-27T21:09:30.000000000Z
                        2020-09-28T00:00:00.000000000Z\t2020-09-27T23:00:00.000000000Z\ta\t80.7879827891636\t2020-09-28T21:15:00.000000000Z
                        2020-11-11T00:00:00.000000000Z\t2020-11-11T00:00:00.000000000Z\ta\t42.602417804870136\t2020-11-11T14:57:30.000000000Z
                        2020-11-12T00:00:00.000000000Z\t2020-11-12T00:00:00.000000000Z\ta\t105.23048053435602\t2020-11-12T21:56:00.000000000Z
                        2020-12-26T00:00:00.000000000Z\t2020-12-26T00:00:00.000000000Z\ta\t94.59407457021454\t2020-12-26T08:45:30.000000000Z
                        2020-12-27T00:00:00.000000000Z\t2020-12-27T00:00:00.000000000Z\ta\tnull\t2020-12-27T22:37:00.000000000Z
                        """,
                "select to_timezone(k, 'Europe/London'), k, s, lat, lon from (select k, s, first(lat) lat, last(k) lon " +
                        "from x " +
                        "where s in ('a') and k in '2020-01-01T00:00:00.000000000Z;2d;45d;48'" +
                        "sample by 1d align to calendar time zone 'Europe/London')",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns('2020-01-01T20:30:00.000000000Z', 35 * 6 * 59 * 1000000000L) k" + // ~3.5 hour interval
                        "   from" +
                        "   long_sequence(365 * 7)" +
                        "),index(s) timestamp(k)",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarWithTimezoneLondon365DaysWithOffset() throws Exception {
        assertSampleByIndexQuery(
                """
                        to_timezone\tk\ts\tlat\tlon
                        2019-12-31T00:51:00.000000000Z\t2019-12-31T00:51:00.000000000Z\ta\t144.77803379943109\t2020-01-01T00:30:00.000000000Z
                        2020-01-01T00:51:00.000000000Z\t2020-01-01T00:51:00.000000000Z\ta\t145.3027002009222\t2020-01-01T03:56:30.000000000Z
                        2020-01-02T00:51:00.000000000Z\t2020-01-02T00:51:00.000000000Z\ta\t0.19935649945118428\t2020-01-02T04:02:00.000000000Z
                        2020-02-15T00:51:00.000000000Z\t2020-02-15T00:51:00.000000000Z\ta\t141.00630662059504\t2020-02-15T01:11:00.000000000Z
                        2020-02-16T00:51:00.000000000Z\t2020-02-16T00:51:00.000000000Z\ta\tnull\t2020-02-16T01:16:30.000000000Z
                        2020-03-31T00:51:00.000000000Z\t2020-03-30T23:51:00.000000000Z\ta\t14.62408950019094\t2020-03-31T05:18:30.000000000Z
                        2020-04-01T00:51:00.000000000Z\t2020-03-31T23:51:00.000000000Z\ta\tnull\t2020-04-01T15:43:30.000000000Z
                        2020-05-15T00:51:00.000000000Z\t2020-05-14T23:51:00.000000000Z\ta\tnull\t2020-05-15T05:59:30.000000000Z
                        2020-05-16T00:51:00.000000000Z\t2020-05-15T23:51:00.000000000Z\ta\t34.633477019382326\t2020-05-16T06:05:00.000000000Z
                        2020-08-13T00:51:00.000000000Z\t2020-08-12T23:51:00.000000000Z\ta\t31.702592206104786\t2020-08-13T07:21:30.000000000Z
                        2020-08-14T00:51:00.000000000Z\t2020-08-13T23:51:00.000000000Z\ta\t52.249166457268934\t2020-08-14T00:34:00.000000000Z
                        2020-09-27T00:51:00.000000000Z\t2020-09-26T23:51:00.000000000Z\ta\tnull\t2020-09-27T11:29:00.000000000Z
                        2020-09-28T00:51:00.000000000Z\t2020-09-27T23:51:00.000000000Z\ta\t2.1077228537622417\t2020-09-28T08:08:00.000000000Z
                        2020-11-11T00:51:00.000000000Z\t2020-11-11T00:51:00.000000000Z\ta\t51.18874172128277\t2020-11-11T01:50:30.000000000Z
                        2020-11-12T00:51:00.000000000Z\t2020-11-12T00:51:00.000000000Z\ta\t42.73330159184082\t2020-11-12T08:49:00.000000000Z
                        2020-12-26T00:51:00.000000000Z\t2020-12-26T00:51:00.000000000Z\ta\t126.10430361638399\t2020-12-26T23:10:30.000000000Z
                        2020-12-27T00:51:00.000000000Z\t2020-12-27T00:51:00.000000000Z\ta\tnull\t2020-12-27T06:03:30.000000000Z
                        """,
                "select to_timezone(k, 'Europe/London'), k, s, lat, lon from (select k, s, last(lat) lat, first(k) lon " +
                        "from x " +
                        "where s in ('a') and k in '2020-01-01T00:00:00.000000000Z;2d;45d;48'" +
                        "sample by 1d align to calendar time zone 'Europe/London' with offset '00:51')",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('b',null,'a') s," +
                        "   timestamp_sequence_ns('2020-01-01 00:30:00', 35 * 6 * 59 * 1000000000L) k" + // ~3.5 hour interval
                        "   from" +
                        "   long_sequence(365 * 7)" +
                        "),index(s) timestamp(k)",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarWithTimezoneLondonShiftBack() throws Exception {
        assertSampleByIndexQuery(
                """
                        to_timezone\tk\ts\tlat\tlon
                        2021-03-26T00:00:00.000000000Z\t2021-03-26T00:00:00.000000000Z\ta\t142.30215575416736\t2021-03-26T22:50:00.000000000Z
                        2021-03-27T00:00:00.000000000Z\t2021-03-27T00:00:00.000000000Z\ta\tnull\t2021-03-27T23:00:00.000000000Z
                        2021-03-28T00:00:00.000000000Z\t2021-03-28T00:00:00.000000000Z\ta\t33.45558404694713\t2021-03-28T20:40:00.000000000Z
                        2021-03-29T00:00:00.000000000Z\t2021-03-28T23:00:00.000000000Z\ta\t70.00560222114518\t2021-03-29T16:40:00.000000000Z
                        2021-03-30T00:00:00.000000000Z\t2021-03-29T23:00:00.000000000Z\ta\t13.290235514836048\t2021-03-30T02:40:00.000000000Z
                        """,
                "select to_timezone(k, 'Europe/London'), k, s, lat, lon from (select k, s, first(lat) lat, last(k) lon " +
                        "from x " +
                        "where s in ('a') " +
                        "sample by 1d align to calendar time zone 'Europe/London')",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns('2021-03-25T23:30:00.000000000Z', 50 * 60 * 1000000000L) k" +
                        "   from" +
                        "   long_sequence(120)" +
                        "),index(s) timestamp(k)",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarWithTimezoneLondonShiftBackwardHourly() throws Exception {
        assertSampleByIndexQuery(
                """
                        to_timezone\tk\ts\tlat\tlastk
                        2021-03-27T21:00:00.000000000Z\t2021-03-27T21:00:00.000000000Z\ta\t132.09083798490755\t2021-03-27T21:51:00.000000000Z
                        2021-03-27T22:00:00.000000000Z\t2021-03-27T22:00:00.000000000Z\ta\t77.68770182183965\t2021-03-27T22:56:00.000000000Z
                        2021-03-27T23:00:00.000000000Z\t2021-03-27T23:00:00.000000000Z\ta\tnull\t2021-03-27T23:48:00.000000000Z
                        2021-03-28T00:00:00.000000000Z\t2021-03-28T00:00:00.000000000Z\ta\t3.6703591550328163\t2021-03-28T00:27:00.000000000Z
                        2021-03-28T02:00:00.000000000Z\t2021-03-28T01:00:00.000000000Z\ta\t94.70222369149758\t2021-03-28T01:45:00.000000000Z
                        2021-03-28T03:00:00.000000000Z\t2021-03-28T02:00:00.000000000Z\ta\t109.23418649425325\t2021-03-28T02:37:00.000000000Z
                        2021-03-28T04:00:00.000000000Z\t2021-03-28T03:00:00.000000000Z\ta\t38.20430552091481\t2021-03-28T03:16:00.000000000Z
                        """,
                "select to_timezone(k, 'Europe/London'), k, s, lat, lastk from (" +
                        "select k, s, first(lat) lat, last(k) lastk " +
                        "from x " +
                        "where s in ('a') and k between '2021-03-27 21:00' and '2021-03-28 04:00'" +
                        "sample by 1h align to calendar time zone 'Europe/London'" +
                        ")",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b') s," +
                        "   timestamp_sequence_ns('2021-03-26T20:30:00.000000000Z', 13 * 60 * 1000000000L) k" +
                        "   from" +
                        "   long_sequence(1000)" +
                        "),index(s) timestamp(k)",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarWithTimezoneLondonShiftForward() throws Exception {
        assertSampleByIndexQuery(
                """
                        to_timezone\tk\ts\tlat\tlon
                        2020-10-23T00:00:00.000000000Z\t2020-10-22T23:00:00.000000000Z\ta\t142.30215575416736\t2020-10-23T22:10:00.000000000Z
                        2020-10-24T00:00:00.000000000Z\t2020-10-23T23:00:00.000000000Z\ta\t7.457062446418488\t2020-10-24T22:20:00.000000000Z
                        2020-10-25T00:00:00.000000000Z\t2020-10-24T23:00:00.000000000Z\ta\tnull\t2020-10-25T20:00:00.000000000Z
                        2020-10-26T00:00:00.000000000Z\t2020-10-26T00:00:00.000000000Z\ta\t33.45558404694713\t2020-10-26T21:50:00.000000000Z
                        2020-10-27T00:00:00.000000000Z\t2020-10-27T00:00:00.000000000Z\ta\t6.612327943200507\t2020-10-27T23:40:00.000000000Z
                        """,
                "select to_timezone(k, 'Europe/London'), k, s, lat, lon from (select k, s, first(lat) lat, last(k) lon " +
                        "from x " +
                        "where s in ('a') " +
                        "sample by 1d align to calendar time zone 'Europe/London')",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns('2020-10-23T20:30:00.000000000Z', 50 * 60 * 1000000000L) k" +
                        "   from" +
                        "   long_sequence(120)" +
                        "),index(s) timestamp(k)",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByAlignToCalendarWithTimezoneLondonShiftForwardHourly() throws Exception {
        assertSampleByIndexQuery(
                """
                        to_timezone\tk\ts\tlat\tlastk
                        2020-10-24T22:00:00.000000000Z\t2020-10-24T21:00:00.000000000Z\ta\t154.93777586404912\t2020-10-24T21:49:28.000000000Z
                        2020-10-24T23:00:00.000000000Z\t2020-10-24T22:00:00.000000000Z\ta\t43.799859246867385\t2020-10-24T22:54:13.000000000Z
                        2020-10-25T00:00:00.000000000Z\t2020-10-24T23:00:00.000000000Z\ta\t38.34194069380561\t2020-10-24T23:41:42.000000000Z
                        2020-10-25T01:00:00.000000000Z\t2020-10-25T00:00:00.000000000Z\ta\t4.158342987512034\t2020-10-25T01:51:12.000000000Z
                        2020-10-25T02:00:00.000000000Z\t2020-10-25T02:00:00.000000000Z\ta\t95.73868763606973\t2020-10-25T02:47:19.000000000Z
                        2020-10-25T03:00:00.000000000Z\t2020-10-25T03:00:00.000000000Z\ta\tnull\t2020-10-25T03:43:26.000000000Z
                        2020-10-25T04:00:00.000000000Z\t2020-10-25T04:00:00.000000000Z\ta\t34.49948946607576\t2020-10-25T04:56:49.000000000Z
                        """,
                "select to_timezone(k, 'Europe/London'), k, s, lat, lastk from (" +
                        "select k, s, first(lat) lat, last(k) lastk " +
                        "from x " +
                        "where s in ('a') and k between '2020-10-24 21:00:00' and '2020-10-25 05:00:00'" +
                        "sample by 1h align to calendar time zone 'Europe/London'" +
                        ")",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b') s," +
                        "   timestamp_sequence_ns('2020-10-23 20:30:00.000000000Z', 259 * 1000000000L) k" +
                        "   from" +
                        "   long_sequence(1000)" +
                        "),index(s) timestamp(k)",
                true,
                true
        );
    }

    @Test
    public void testIndexSampleByBufferExceeded() throws Exception {
        node1.setProperty(PropertyKey.CAIRO_SQL_SAMPLEBY_PAGE_SIZE, 16);

        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where s in ('a')" +
                        "sample by 60s align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 4096) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T00:01:00.000000000Z\ta\t-2.0\t2.0
                        1970-01-01T00:03:00.000000000Z\ta\t-4.0\t4.0
                        1970-01-01T00:05:00.000000000Z\ta\t-6.0\t6.0
                        1970-01-01T00:07:00.000000000Z\ta\t-8.0\t8.0
                        1970-01-01T00:09:00.000000000Z\ta\t-10.0\t10.0
                        1970-01-01T00:11:00.000000000Z\ta\t-12.0\t12.0
                        1970-01-01T00:13:00.000000000Z\ta\t-14.0\t14.0
                        1970-01-01T00:15:00.000000000Z\ta\t-16.0\t16.0
                        1970-01-01T00:17:00.000000000Z\ta\t-18.0\t18.0
                        1970-01-01T00:19:00.000000000Z\ta\t-20.0\t20.0
                        1970-01-01T00:21:00.000000000Z\ta\t-22.0\t22.0
                        1970-01-01T00:23:00.000000000Z\ta\t-24.0\t24.0
                        1970-01-01T00:25:00.000000000Z\ta\t-26.0\t26.0
                        1970-01-01T00:27:00.000000000Z\ta\t-28.0\t28.0
                        1970-01-01T00:29:00.000000000Z\ta\t-30.0\t30.0
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where s in ('a')" +
                        "sample by 2m align to first observation",
                """
                        insert into xx \
                        select -x lat,
                        x lon,
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(0, 60 * 1000L * 1000L) k
                        from
                        long_sequence(30)
                        """
        );

        assertWithSymbolColumnTop(
                """
                        k\ts\tlat\tlon
                        1970-01-01T00:00:00.000000000Z\t\t-1.0\t10.0
                        1970-01-01T00:10:00.000000000Z\t\t-11.0\t20.0
                        1970-01-01T00:20:00.000000000Z\t\t-21.0\t30.0
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where s = null " +
                        "sample by 10m align to first observation"
        );
    }

    @Test
    public void testIndexSampleByEmpty() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k > '2000-01-04' and s in ('a') " +
                        "sample by 1h align to first observation",
                "create table xx as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns(172800000000000, 1000000000000) k" +
                        "   from" +
                        "   long_sequence(100)" +
                        "), index(s capacity 10) timestamp(k) partition by DAY",
                "k",
                false,
                false
        );

        assertWithSymbolColumnTop(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k > '2000-01-04' and s = null " +
                        "sample by 1h align to first observation"
        );
    }

    @Test
    public void testIndexSampleByFirstAndLast() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k in '1970-02' and s in ('b')" +
                        "sample by 2h align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 10) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-02-01T00:00:00.000000000Z\tb\t-745.0\t767.0
                        1970-02-02T00:00:00.000000000Z\tb\t-769.0\t791.0
                        1970-02-03T00:00:00.000000000Z\tb\t-793.0\t815.0
                        1970-02-04T00:00:00.000000000Z\tb\t-817.0\t839.0
                        1970-02-05T00:00:00.000000000Z\tb\t-841.0\t863.0
                        1970-02-06T00:00:00.000000000Z\tb\t-865.0\t887.0
                        1970-02-07T00:00:00.000000000Z\tb\t-889.0\t911.0
                        1970-02-08T00:00:00.000000000Z\tb\t-913.0\t935.0
                        1970-02-09T00:00:00.000000000Z\tb\t-937.0\t959.0
                        1970-02-10T00:00:00.000000000Z\tb\t-961.0\t983.0
                        1970-02-11T00:00:00.000000000Z\tb\t-985.0\t1007.0
                        1970-02-12T00:00:00.000000000Z\tb\t-1009.0\t1031.0
                        1970-02-13T00:00:00.000000000Z\tb\t-1033.0\t1055.0
                        1970-02-14T00:00:00.000000000Z\tb\t-1057.0\t1079.0
                        1970-02-15T00:00:00.000000000Z\tb\t-1081.0\t1103.0
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k in '1970-02' and k < '1970-02-16' and s in ('b')" +
                        "sample by 1d align to first observation",
                "insert into xx " +
                        "select -x lat,\n" +
                        "x lon,\n" +
                        "(case when x % 2 = 0 then 'a' else 'b' end) s,\n" +
                        "timestamp_sequence(0, 60 * 60 * 1000000L) k\n" + // 60 mins
                        "from\n" +
                        "long_sequence(365 * 24)\n"
        );

        assertWithSymbolColumnTop(
                """
                        k\ts\tlat\tlon
                        1970-02-01T00:00:00.000000000Z\t\t-745.0\t816.0
                        1970-02-04T00:00:00.000000000Z\t\t-817.0\t888.0
                        1970-02-07T00:00:00.000000000Z\t\t-889.0\t960.0
                        1970-02-10T00:00:00.000000000Z\t\t-961.0\t1032.0
                        1970-02-13T00:00:00.000000000Z\t\t-1033.0\t1104.0
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k in '1970-02' and k < '1970-02-16' and s = null " +
                        "sample by 3d align to first observation"
        );
    }

    @Test
    public void testIndexSampleByIndexFrameExceedsPartitionFrame() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 256) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T00:10:00.000000000Z\ta\t-2.0\t2.0
                        1970-01-01T04:10:00.000000000Z\ta\t-32.0\t32.0
                        1970-01-01T10:10:00.000000000Z\ta\t-62.0\t62.0
                        1970-01-01T14:10:00.000000000Z\ta\t-92.0\t92.0
                        1970-01-01T20:10:00.000000000Z\ta\t-122.0\t122.0
                        1970-01-02T00:10:00.000000000Z\ta\t-152.0\t152.0
                        """,
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a', 'none')" +
                        "sample by 2h align to first observation",
                """
                        insert into xx \
                        select -x lat,
                        x lon,
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(0, 10 * 60 * 1000000L) k
                        from
                        long_sequence(180)
                        """
        );

        assertWithSymbolColumnTop(
                """
                        k\ts\tlat\tlon
                        1970-01-01T00:00:00.000000000Z\t\t-1.0\t4.0
                        1970-01-01T04:00:00.000000000Z\t\t-31.0\t34.0
                        1970-01-01T10:00:00.000000000Z\t\t-61.0\t64.0
                        1970-01-01T14:00:00.000000000Z\t\t-91.0\t94.0
                        1970-01-01T20:00:00.000000000Z\t\t-121.0\t124.0
                        1970-01-02T00:00:00.000000000Z\t\t-151.0\t154.0
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s = null " +
                        "sample by 2h align to first observation"
        );
    }

    @Test
    public void testIndexSampleByIndexNoTimestampColSelected() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                    ", index(s capacity 256) timestamp(k) partition by DAY");

            assertQueryNoLeakCheck(
                    """
                            s\tlat\tlon
                            a\t-2.0\t2.0
                            a\t-32.0\t32.0
                            a\t-62.0\t62.0
                            a\t-92.0\t92.0
                            a\t-122.0\t122.0
                            a\t-152.0\t152.0
                            """,
                    "select s, first(lat) lat, first(lon) lon " +
                            "from xx " +
                            "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                            "sample by 2h align to first observation",
                    """
                            insert into xx
                            select -x lat,
                            x lon,
                            (case when x % 2 = 0 then 'a' else 'b' end) s,
                            timestamp_sequence(0, 10 * 60 * 1000000L) k
                            from
                            long_sequence(180)
                            """,
                    null,
                    false,
                    false
            );

            execute("alter table xx drop column s", sqlExecutionContext);
            execute("alter table xx add s SYMBOL INDEX", sqlExecutionContext);

            TestUtils.assertSqlCursors(
                    engine,
                    sqlExecutionContext,
                    "select s, first(lat) lat, last(lon) lon " +
                            "from xx " +
                            "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10'" +
                            "sample by 2h align to first observation",
                    "select s, first(lat) lat, last(lon) lon " +
                            "from xx " +
                            "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s = null " +
                            "sample by 2h align to first observation",
                    LOG
            );
        });
    }

    @Test
    public void testIndexSampleByIndexWithIrregularEmptyPeriods() throws Exception {
        assertMemoryLeak(() -> execute("create table xx (s symbol, k timestamp_ns)" +
                ", index(s capacity 256) timestamp(k) partition by DAY"));

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T20:50:00.000000000Z\ta\t1970-01-01T20:50:00.000000000Z\t1970-01-01T21:30:00.000000000Z
                        1970-01-01T21:50:00.000000000Z\ta\t1970-01-01T21:50:00.000000000Z\t1970-01-01T21:50:00.000000000Z
                        1970-01-01T23:50:00.000000000Z\ta\t1970-01-02T00:30:00.000000000Z\t1970-01-02T00:30:00.000000000Z
                        1970-01-02T00:50:00.000000000Z\ta\t1970-01-02T00:50:00.000000000Z\t1970-01-02T01:10:00.000000000Z
                        1970-01-02T03:50:00.000000000Z\ta\t1970-01-02T03:50:00.000000000Z\t1970-01-02T03:50:00.000000000Z
                        """,
                "select k, s, first(k) lat, last(k) lon " +
                        "from xx " +
                        "where k between '1970-01-01T20:00' and '1970-01-02T04:00' and s in ('a')" +
                        "sample by 1h align to first observation",
                """
                        insert into xx \
                        select \
                        (case when (x / 7) % 3 = 0 and x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(0, 10 * 60 * 1000000L) k
                        from
                        long_sequence(360)
                        """
        );

        assertWithSymbolColumnTop(
                """
                        k\ts\tlat\tlon
                        1970-01-01T20:00:00.000000000Z\t\t1970-01-01T20:00:00.000000000Z\t1970-01-01T21:50:00.000000000Z
                        1970-01-01T22:00:00.000000000Z\t\t1970-01-01T22:00:00.000000000Z\t1970-01-01T23:50:00.000000000Z
                        1970-01-02T00:00:00.000000000Z\t\t1970-01-02T00:00:00.000000000Z\t1970-01-02T01:50:00.000000000Z
                        1970-01-02T02:00:00.000000000Z\t\t1970-01-02T02:00:00.000000000Z\t1970-01-02T03:50:00.000000000Z
                        1970-01-02T04:00:00.000000000Z\t\t1970-01-02T04:00:00.000000000Z\t1970-01-02T04:00:00.000000000Z
                        """,
                "select k, s, first(k) lat, last(k) lon " +
                        "from xx " +
                        "where k between '1970-01-01T20:00' and '1970-01-02T04:00' and s = null " +
                        "sample by 2h align to first observation"
        );
    }

    @Test
    public void testIndexSampleByLastAndFirstOnDifferentIndexPages() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01T00:00:00.000000000Z;30m;5h;10' and s in ('a')" +
                        "sample by 2h align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 10) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T21:10:00.000000000Z\ta\t-128.0\t138.0
                        1970-01-01T23:10:00.000000000Z\ta\t-140.0\t150.0
                        1970-01-02T01:10:00.000000000Z\ta\t-152.0\t162.0
                        1970-01-02T03:10:00.000000000Z\ta\t-164.0\t174.0
                        1970-01-02T05:10:00.000000000Z\ta\t-176.0\t180.0
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k > '1970-01-01T21:00' and s in ('a')" +
                        "sample by 2h align to first observation",
                """
                        insert into xx \
                        select -x lat,
                        x lon,
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(0, 10 * 60 * 1000000L) k
                        from
                        long_sequence(180)
                        """
        );

        assertWithSymbolColumnTop(
                """
                        k\ts\tlat\tlon
                        1970-01-01T21:10:00.000000000Z\t\t-128.0\t139.0
                        1970-01-01T23:10:00.000000000Z\t\t-140.0\t151.0
                        1970-01-02T01:10:00.000000000Z\t\t-152.0\t163.0
                        1970-01-02T03:10:00.000000000Z\t\t-164.0\t175.0
                        1970-01-02T05:10:00.000000000Z\t\t-176.0\t180.0
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k > '1970-01-01T21:00' and s = null " +
                        "sample by 2h align to first observation"
        );
    }

    @Test
    public void testIndexSampleByManyPartitions() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k in '1970-02' and s in ('b')" +
                        "sample by 2h align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 10) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-02-01T00:00:00.000000000Z\tb\t-745.0\t745.0
                        1970-02-02T00:00:00.000000000Z\tb\t-769.0\t769.0
                        1970-02-03T00:00:00.000000000Z\tb\t-793.0\t793.0
                        1970-02-04T00:00:00.000000000Z\tb\t-817.0\t817.0
                        1970-02-05T00:00:00.000000000Z\tb\t-841.0\t841.0
                        1970-02-06T00:00:00.000000000Z\tb\t-865.0\t865.0
                        1970-02-07T00:00:00.000000000Z\tb\t-889.0\t889.0
                        1970-02-08T00:00:00.000000000Z\tb\t-913.0\t913.0
                        1970-02-09T00:00:00.000000000Z\tb\t-937.0\t937.0
                        1970-02-10T00:00:00.000000000Z\tb\t-961.0\t961.0
                        1970-02-11T00:00:00.000000000Z\tb\t-985.0\t985.0
                        1970-02-12T00:00:00.000000000Z\tb\t-1009.0\t1009.0
                        1970-02-13T00:00:00.000000000Z\tb\t-1033.0\t1033.0
                        1970-02-14T00:00:00.000000000Z\tb\t-1057.0\t1057.0
                        1970-02-15T00:00:00.000000000Z\tb\t-1081.0\t1081.0
                        """,
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k in '1970-02' and k < '1970-02-16' and s in ('b')" +
                        "sample by 1d align to first observation",
                "insert into xx " +
                        "select -x lat,\n" +
                        "x lon,\n" +
                        "(case when x % 2 = 0 then 'a' else 'b' end) s,\n" +
                        "timestamp_sequence(0, 60 * 60 * 1000000L) k\n" + // 60 mins
                        "from\n" +
                        "long_sequence(365 * 24)\n"
        );
    }

    @Test
    public void testIndexSampleByMicro() throws Exception {
        node1.setProperty(PropertyKey.CAIRO_SQL_SAMPLEBY_PAGE_SIZE, 256);
        assertSampleByIndexQuery(
                """
                        k\tfirst
                        2021-01-01T00:07:39.760000000Z\t15318
                        2021-01-01T00:07:40.560000000Z\t15341
                        2021-01-01T00:07:40.970000000Z\t15355
                        2021-01-01T00:07:41.090000000Z\t15359
                        2021-01-01T00:07:42.600000000Z\t15410
                        2021-01-01T00:07:42.890000000Z\t15420
                        2021-01-01T00:07:43.080000000Z\t15425
                        2021-01-01T00:07:43.400000000Z\t15436
                        2021-01-01T00:07:43.440000000Z\t15437
                        2021-01-01T00:07:43.520000000Z\t15439
                        2021-01-01T00:07:43.550000000Z\t15440
                        2021-01-01T00:07:43.980000000Z\t15458
                        2021-01-01T00:07:45.170000000Z\t15497
                        2021-01-01T00:07:45.250000000Z\t15500
                        2021-01-01T00:07:45.660000000Z\t15513
                        2021-01-01T00:07:46.440000000Z\t15539
                        2021-01-01T00:07:46.640000000Z\t15544
                        2021-01-01T00:07:48.000000000Z\t15587
                        2021-01-01T00:07:49.010000000Z\t15620
                        2021-01-01T00:07:49.240000000Z\t15627
                        2021-01-01T00:07:49.520000000Z\t15636
                        2021-01-01T00:07:49.620000000Z\t15639
                        2021-01-01T00:07:49.800000000Z\t15647
                        2021-01-01T00:07:50.290000000Z\t15665
                        2021-01-01T00:07:51.360000000Z\t15699
                        2021-01-01T00:07:51.470000000Z\t15703
                        2021-01-01T00:07:51.880000000Z\t15716
                        2021-01-01T00:07:51.930000000Z\t15717
                        2021-01-01T00:07:52.140000000Z\t15724
                        2021-01-01T00:07:52.390000000Z\t15732
                        2021-01-01T00:07:52.470000000Z\t15734
                        2021-01-01T00:07:52.910000000Z\t15748
                        2021-01-01T00:07:53.070000000Z\t15754
                        2021-01-01T00:07:53.110000000Z\t15756
                        2021-01-01T00:07:53.960000000Z\t15789
                        2021-01-01T00:07:54.540000000Z\t15810
                        2021-01-01T00:07:55.270000000Z\t15838
                        2021-01-01T00:07:55.340000000Z\t15841
                        2021-01-01T00:07:55.630000000Z\t15852
                        2021-01-01T00:07:55.680000000Z\t15854
                        2021-01-01T00:07:56.640000000Z\t15883
                        2021-01-01T00:07:57.150000000Z\t15895
                        2021-01-01T00:07:58.440000000Z\t15939
                        2021-01-01T00:07:58.600000000Z\t15944
                        2021-01-01T00:07:58.760000000Z\t15949
                        2021-01-01T00:07:58.980000000Z\t15958
                        2021-01-01T00:07:59.270000000Z\t15966
                        2021-01-01T00:08:00.370000000Z\t15999
                        2021-01-01T00:08:00.630000000Z\t16008
                        2021-01-01T00:08:00.670000000Z\t16009
                        2021-01-01T00:08:00.710000000Z\t16011
                        2021-01-01T00:08:01.270000000Z\t16033
                        2021-01-01T00:08:02.180000000Z\t16072
                        2021-01-01T00:08:02.280000000Z\t16076
                        2021-01-01T00:08:02.860000000Z\t16099
                        2021-01-01T00:08:02.880000000Z\t16100
                        2021-01-01T00:08:06.660000000Z\t16227
                        2021-01-01T00:08:06.720000000Z\t16229
                        2021-01-01T00:08:07.720000000Z\t16257
                        2021-01-01T00:08:08.830000000Z\t16296
                        2021-01-01T00:08:10.800000000Z\t16367
                        2021-01-01T00:08:10.830000000Z\t16368
                        2021-01-01T00:08:11.170000000Z\t16382
                        2021-01-01T00:08:11.590000000Z\t16403
                        2021-01-01T00:08:12.040000000Z\t16418
                        2021-01-01T00:08:12.090000000Z\t16419
                        2021-01-01T00:08:13.840000000Z\t16480
                        2021-01-01T00:08:13.890000000Z\t16482
                        2021-01-01T00:08:14.000000000Z\t16485
                        2021-01-01T00:08:14.420000000Z\t16499
                        2021-01-01T00:08:16.410000000Z\t16567
                        2021-01-01T00:08:16.890000000Z\t16583
                        2021-01-01T00:08:16.990000000Z\t16586
                        2021-01-01T00:08:18.180000000Z\t16628
                        2021-01-01T00:08:18.710000000Z\t16645
                        2021-01-01T00:08:19.190000000Z\t16661
                        2021-01-01T00:08:20.110000000Z\t16698
                        2021-01-01T00:08:20.240000000Z\t16702
                        2021-01-01T00:08:20.710000000Z\t16718
                        2021-01-01T00:08:21.040000000Z\t16729
                        2021-01-01T00:08:21.510000000Z\t16744
                        2021-01-01T00:08:22.940000000Z\t16788
                        2021-01-01T00:08:22.990000000Z\t16789
                        2021-01-01T00:08:23.370000000Z\t16801
                        2021-01-01T00:08:23.420000000Z\t16802
                        2021-01-01T00:08:23.560000000Z\t16807
                        2021-01-01T00:08:23.720000000Z\t16811
                        2021-01-01T00:08:23.990000000Z\t16818
                        2021-01-01T00:08:24.010000000Z\t16820
                        2021-01-01T00:08:24.650000000Z\t16840
                        2021-01-01T00:08:25.070000000Z\t16853
                        2021-01-01T00:08:25.870000000Z\t16879
                        2021-01-01T00:08:26.070000000Z\t16887
                        2021-01-01T00:08:27.110000000Z\t16920
                        2021-01-01T00:08:27.310000000Z\t16926
                        2021-01-01T00:08:27.350000000Z\t16927
                        2021-01-01T00:08:27.850000000Z\t16940
                        2021-01-01T00:08:28.020000000Z\t16945
                        2021-01-01T00:08:28.250000000Z\t16951
                        2021-01-01T00:08:29.420000000Z\t16994
                        """,
                """
                        select k, first(lat)
                        from sam_by_tst
                        where s in ('a')
                        sample by 10T align to first observation  limit -100
                        """,
                """
                        create table sam_by_tst as (
                        select rnd_symbol('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g') as s,
                        timestamp_sequence_ns('2021-01-01', rnd_short(1,5) * 10000000L) as k,
                        x as lat,
                        -x as lon
                        from long_sequence(17 * 1000L)
                        ), index(s) timestamp(k) partition by DAY""",
                false,
                true
        );
    }

    @Test
    public void testIndexSampleByMonth() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, last(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where s = 'b' " +
                        "  and k >= cast(1388534400 * 1000000L as timestamp) " +
                        "  and k <= cast(1655742718 * 1000000L as timestamp)" +
                        "sample by 1M align to first observation ",
                "create table xx (k timestamp_ns, s symbol, lat double, lon double)" +
                        ", index(s capacity 10) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        2014-01-01T00:00:00.000000000Z\tb\t248.0\t123.7
                        """,
                "select k, s, last(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where s = 'b' " +
                        "  and k >= cast(1388534400 * 1000000L as timestamp) " +
                        "  and k <= cast(1655742718 * 1000000L as timestamp)" +
                        "sample by 1M align to first observation",
                "insert into xx " +
                        "values " +
                        "    ('2014-01-01T00:00:00.000000000Z', 'b', 245, 123.4)," +
                        "    ('2014-01-01T00:05:00.000000000Z', 'b', 246, 123.5)," +
                        "    ('2014-01-01T00:10:00.000000000Z', 'b', 247, 123.6)," +
                        "    ('2014-01-01T00:15:00.000000000Z', 'b', 248, 123.7);"
        );
    }

    @Test
    public void testIndexSampleBySameTimePoints() throws Exception {
        assertQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T00:00:00.000000000Z\ta\t1\t58
                        1970-01-01T01:00:00.000000000Z\ta\t63\t116
                        1970-01-01T02:00:00.000000000Z\ta\t126\t178
                        1970-01-01T03:00:00.000000000Z\ta\t184\t238
                        1970-01-01T04:00:00.000000000Z\ta\t240\t299
                        """,
                "select k, s, first(lat) lat, last(lat) lon " +
                        "from x " +
                        "where k between '1970-01-01' and '1970-01-01T04:00' and s in ('a') " +
                        "sample by 1h align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        "   x lat," +
                        "   x lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   cast(((x / 60L) * 1000000L * 60L * 60L * 1000L) as timestamp_ns) k" +
                        "   from" +
                        "   long_sequence(25*60)" +
                        "), index(s) timestamp(k) partition by DAY",
                "k",
                false
        );
    }

    @Test
    public void testIndexSampleByVeryFewRowsPerInterval() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from xx " +
                        "where k in '1970-02' and s in ('a')" +
                        "sample by 2h align to first observation",
                "create table xx (lat long, lon long, s symbol, k timestamp_ns)" +
                        ", index(s capacity 10) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T00:54:00.000000000Z\ta\t-2\t2
                        1970-01-01T02:39:00.000000000Z\ta\t-4\t4
                        1970-01-01T04:29:00.000000000Z\ta\t-6\t6
                        1970-01-01T06:14:00.000000000Z\ta\t-8\t8
                        1970-01-01T08:04:00.000000000Z\ta\t-10\t10
                        1970-01-01T09:54:00.000000000Z\ta\t-12\t12
                        1970-01-01T11:39:00.000000000Z\ta\t-14\t14
                        1970-01-01T13:29:00.000000000Z\ta\t-16\t16
                        1970-01-01T15:14:00.000000000Z\ta\t-18\t18
                        1970-01-01T17:04:00.000000000Z\ta\t-20\t20
                        1970-01-01T18:54:00.000000000Z\ta\t-22\t22
                        1970-01-01T20:39:00.000000000Z\ta\t-24\t24
                        1970-01-01T22:29:00.000000000Z\ta\t-26\t26
                        """,
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k in '1970-01-01' and s in ('a')" +
                        "sample by 5m align to first observation",
                "insert into xx " +
                        "select -x lat,\n" +
                        "x lon,\n" +
                        "(case when x % 2 = 0 then 'a' else 'b' end) s,\n" +
                        "timestamp_sequence(0, 54 * 60 * 1000000L) k\n" + // 54 mins
                        "from\n" +
                        "long_sequence(48)\n"
        );
    }

    @Test
    public void testIndexSampleByWithArithmetics() throws Exception {
        assertQuery(
                """
                        k\ts\tlat\tlon\tconst
                        1970-01-04T00:26:40.000000000Z\ta\t71.00560222114518\t336.09942524982637\t1
                        1970-01-04T01:26:40.000000000Z\ta\t7.612327943200507\t302.609357768427\t1
                        1970-01-04T02:26:40.000000000Z\ta\t118.11888283070247\tnull\t1
                        1970-01-04T03:26:40.000000000Z\ta\t100.02039650915859\t256.84202790934114\t1
                        """,
                "select k, s, first(lat) + 1 lat, last(lon) * 2 lon, 1 as const " +
                        "from x " +
                        "where k > '1970-01-04' and s in ('a') " +
                        "sample by 1h align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns(172800000000000, 1000000000000) k" +
                        "   from" +
                        "   long_sequence(100)" +
                        "), index(s capacity 10) timestamp(k) partition by DAY",
                "k",
                false
        );
    }

    @Test
    public void testIndexSampleByWithEmptyIndexPage() throws Exception {
        assertQuery(
                "k\ts\tlat\tlon\n",
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k in '1970-02' and s in ('b')" +
                        "sample by 2h align to first observation",
                "create table xx (lat double, lon double, s symbol, k timestamp_ns)" +
                        ", index(s capacity 10) timestamp(k) partition by DAY",
                "k",
                false,
                true
        );

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-02-02T00:00:00.000000000Z\tb\t-33.0\t33.0
                        1970-02-04T00:00:00.000000000Z\tb\t-35.0\t35.0
                        1970-02-06T00:00:00.000000000Z\tb\t-37.0\t37.0
                        1970-02-08T00:00:00.000000000Z\tb\t-39.0\t39.0
                        1970-02-10T00:00:00.000000000Z\tb\t-41.0\t41.0
                        1970-02-12T00:00:00.000000000Z\tb\t-43.0\t43.0
                        1970-02-14T00:00:00.000000000Z\tb\t-45.0\t45.0
                        """,
                "select k, s, first(lat) lat, first(lon) lon " +
                        "from xx " +
                        "where k in '1970-02' and k < '1970-02-16' and s in ('b')" +
                        "sample by 1d align to first observation",
                "insert into xx " +
                        "select -x lat,\n" +
                        "x lon,\n" +
                        "(case when x % 2 = 0 then 'a' else 'b' end) s,\n" +
                        "timestamp_sequence(0, 24 * 60 * 60 * 1000000L) k\n" + // 60 mins
                        "from\n" +
                        "long_sequence(365)\n"
        );
    }

    @Test
    public void testIndexSampleByWithInvalidFunctionArgs() throws Exception {
        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-04T00:26:40.000000000Z\ta\t71.00560222114518\t336.09942524982637
                        1970-01-04T01:26:40.000000000Z\ta\t7.612327943200507\t302.609357768427
                        1970-01-04T02:26:40.000000000Z\ta\t118.11888283070247\tnull
                        1970-01-04T03:26:40.000000000Z\ta\t100.02039650915859\t256.84202790934114
                        """,
                "select k, s, first(lat + 1) lat, last(lon * 2) lon " +
                        "from x " +
                        "where k > '1970-01-04' and s in ('a') " +
                        "sample by 1h align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns(172800000000000, 1000000000000) k" +
                        "   from" +
                        "   long_sequence(100)" +
                        "), index(s capacity 10) timestamp(k) partition by DAY"
        );
    }

    @Test
    public void testIndexSampleByWithInvalidFunctionArgs2() throws Exception {
        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-04T00:26:40.000000000Z\ta\t70.00560222114518\t1
                        1970-01-04T01:26:40.000000000Z\ta\t6.612327943200507\t1
                        1970-01-04T02:26:40.000000000Z\ta\t117.11888283070247\t1
                        1970-01-04T03:26:40.000000000Z\ta\t99.02039650915859\t1
                        """,
                "select k, s, first(lat) lat, last(1) lon " +
                        "from x " +
                        "where k > '1970-01-04' and s in ('a') " +
                        "sample by 1h align to first observation ",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns(172800000000000, 1000000000000) k" +
                        "   from" +
                        "   long_sequence(100)" +
                        "), index(s capacity 10) timestamp(k) partition by DAY"
        );
    }

    @Test
    public void testIndexSampleIndexNoRowsInIndex() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table xx (k timestamp_ns)\n" +
                    " timestamp(k) partition by DAY");
            execute(
                    """
                            insert into xx \
                            select \
                            timestamp_sequence(0, 1 * 60 * 1000000L) k
                            from
                            long_sequence(100)
                            """);
            execute("alter table xx add s SYMBOL INDEX", sqlExecutionContext);

            String expected = """
                    fk\tlk\tk\ts
                    1970-01-01T00:00:00.000000000Z\t1970-01-01T00:59:00.000000000Z\t1970-01-01T00:00:00.000000000Z\t
                    1970-01-01T01:00:00.000000000Z\t1970-01-01T01:39:00.000000000Z\t1970-01-01T01:00:00.000000000Z\t
                    """;

            // Forced no index execution
            assertSql(expected, """
                    select first(k) fk, last(k) lk, k, s
                    from xx
                    where s = null or s = 'none'
                    sample by 1h"""
            );

            // Indexed execution
            assertSql(expected, """
                    select first(k) fk, last(k) lk, k, s
                    from xx
                    where s = null
                    sample by 1h"""
            );
        });
    }

    @Test
    public void testIndexSampleLatestRestrictedByWhere() throws Exception {
        assertMemoryLeak(() -> execute("create table xx (s symbol, k timestamp_ns)" +
                ", index(s capacity 256) timestamp(k) partition by DAY"));

        assertSampleByIndexQuery(
                """
                        k\ts\tlat\tlon
                        1970-01-01T05:01:00.000000000Z\ta\t1970-01-01T05:01:00.000000000Z\t1970-01-01T05:29:00.000000000Z
                        """,
                "select k, s, first(k) lat, last(k) lon " +
                        "from xx " +
                        "where k between '1970-01-01T05:00' and '1970-01-01T05:30' and s in ('a')" +
                        "sample by 1h align to first observation",
                """
                        insert into xx \
                        select \
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(0, 1 * 60 * 1000000L) k
                        from
                        long_sequence(360)
                        """
        );
    }

    @Test
    public void testIndexSampleMainIndexHasColumnTop() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table xx (k timestamp_ns)\n" +
                    " timestamp(k) partition by DAY");
            execute(
                    """
                            insert into xx \
                            select \
                            timestamp_sequence('1970-01-01T12', 2 * 60 * 60 * 1000000L) k
                            from
                            long_sequence(8)
                            """);
            execute("alter table xx add s SYMBOL INDEX", sqlExecutionContext);
            execute("""
                    insert into xx \
                    select \
                    timestamp_sequence('1970-01-03', 1 * 60 * 1000000L),
                    (case when x % 2 = 0 then 'a' else 'b' end) sk
                    from
                    long_sequence(60)
                    """);
        });

        // 1970-01-01 data does not have s column
        // first hour of 1970-01-02 does not have s column
        assertSampleByIndexQuery(
                """
                        fk\tlk\tk\ts
                        1970-01-02T01:00:00.000000000Z\t1970-01-02T01:58:00.000000000Z\t1970-01-02T01:00:00.000000000Z\tb
                        1970-01-02T02:00:00.000000000Z\t1970-01-02T02:58:00.000000000Z\t1970-01-02T02:00:00.000000000Z\tb
                        1970-01-02T03:00:00.000000000Z\t1970-01-02T03:58:00.000000000Z\t1970-01-02T03:00:00.000000000Z\tb
                        1970-01-02T04:00:00.000000000Z\t1970-01-02T04:58:00.000000000Z\t1970-01-02T04:00:00.000000000Z\tb
                        1970-01-02T05:00:00.000000000Z\t1970-01-02T05:58:00.000000000Z\t1970-01-02T05:00:00.000000000Z\tb
                        1970-01-03T00:00:00.000000000Z\t1970-01-03T00:58:00.000000000Z\t1970-01-03T00:00:00.000000000Z\tb
                        """,
                "select first(k) fk, last(k) lk, k, s\n" +
                        "from xx " +
                        "where s in ('b')" +
                        "sample by 1h align to first observation",
                """
                        insert into xx \
                        select \
                        timestamp_sequence('1970-01-02T01', 1 * 60 * 1000000L),
                        (case when x % 2 = 0 then 'a' else 'b' end) sk
                        from
                        long_sequence(300)
                        """
        );
    }

    @Test
    public void testIndexSampleWithColumnTops() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table xx (s symbol, k timestamp_ns)" +
                    ", index(s capacity 256) timestamp(k) partition by DAY");

            execute(
                    """
                            insert into xx \
                            select \
                            (case when x % 2 = 0 then 'a' else 'b' end) s,
                            timestamp_sequence(0, 1 * 60 * 1000000L) k
                            from
                            long_sequence(100)
                            """);

            execute("alter table xx add i1 int", sqlExecutionContext);
            execute("alter table xx add c1 char", sqlExecutionContext);
            execute("alter table xx add l1 long", sqlExecutionContext);

            execute(
                    """
                            insert into xx \
                            select \
                            (case when x % 2 = 0 then 'a' else 'b' end) s,
                            timestamp_sequence(100 * 60 * 1000000L, 1 * 60 * 1000000L) k,
                            cast(x + 100 as int) i1,\s
                            rnd_char() c1,
                            x as l1
                            from
                            long_sequence(100)""");

            execute("alter table xx add f1 float", sqlExecutionContext);
            execute("alter table xx add d1 double", sqlExecutionContext);
            execute("alter table xx add s1 symbol", sqlExecutionContext);
            execute("alter table xx add ss1 short", sqlExecutionContext);
            execute("alter table xx add b1 byte", sqlExecutionContext);
            execute("alter table xx add t1 timestamp_ns", sqlExecutionContext);
            execute("alter table xx add dt date", sqlExecutionContext);
        });

        assertSampleByIndexQuery(
                """
                        fi1\tli1\tfc1\tlc1\tfl1\tll1\tff1\tlf1\tfd1\tld1\tfs1\tls1\tfss1\tlss1\tfb1\tlb1\tfk\tlk\tft1\tlt1\tfdt\tldt\tk\ts
                        null\tnull\t\t\tnull\tnull\tnull\tnull\tnull\tnull\t\t\t0\t0\t0\t0\t1970-01-01T00:00:00.000000000Z\t1970-01-01T00:28:00.000000000Z\t\t\t\t\t1970-01-01T00:00:00.000000000Z\tb
                        null\tnull\t\t\tnull\tnull\tnull\tnull\tnull\tnull\t\t\t0\t0\t0\t0\t1970-01-01T00:30:00.000000000Z\t1970-01-01T00:58:00.000000000Z\t\t\t\t\t1970-01-01T00:30:00.000000000Z\tb
                        null\tnull\t\t\tnull\tnull\tnull\tnull\tnull\tnull\t\t\t0\t0\t0\t0\t1970-01-01T01:00:00.000000000Z\t1970-01-01T01:28:00.000000000Z\t\t\t\t\t1970-01-01T01:00:00.000000000Z\tb
                        null\t119\t\tG\tnull\t19\tnull\tnull\tnull\tnull\t\t\t0\t0\t0\t0\t1970-01-01T01:30:00.000000000Z\t1970-01-01T01:58:00.000000000Z\t\t\t\t\t1970-01-01T01:30:00.000000000Z\tb
                        121\t149\tS\tL\t21\t49\tnull\tnull\tnull\tnull\t\t\t0\t0\t0\t0\t1970-01-01T02:00:00.000000000Z\t1970-01-01T02:28:00.000000000Z\t\t\t\t\t1970-01-01T02:00:00.000000000Z\tb
                        151\t179\tD\tR\t51\t79\tnull\tnull\tnull\tnull\t\t\t0\t0\t0\t0\t1970-01-01T02:30:00.000000000Z\t1970-01-01T02:58:00.000000000Z\t\t\t\t\t1970-01-01T02:30:00.000000000Z\tb
                        181\t209\tZ\tV\t81\t109\tnull\t204.5\tnull\t222.5\t\tc3\t0\t9\t0\t9\t1970-01-01T03:00:00.000000000Z\t1970-01-01T03:28:00.000000000Z\t\t1970-01-01T00:00:00.000009000Z\t\t1970-01-01T00:00:00.009Z\t1970-01-01T03:00:00.000000000Z\tb
                        211\t239\tD\tT\t111\t139\t205.5\t219.5\t227.5\t297.5\t\t\t11\t39\t11\t39\t1970-01-01T03:30:00.000000000Z\t1970-01-01T03:58:00.000000000Z\t1970-01-01T00:00:00.000011000Z\t1970-01-01T00:00:00.000039000Z\t1970-01-01T00:00:00.011Z\t1970-01-01T00:00:00.039Z\t1970-01-01T03:30:00.000000000Z\tb
                        241\t269\tS\tL\t141\t169\t220.5\t234.5\t302.5\t372.5\tc3\tc3\t41\t69\t41\t69\t1970-01-01T04:00:00.000000000Z\t1970-01-01T04:28:00.000000000Z\t1970-01-01T00:00:00.000041000Z\t1970-01-01T00:00:00.000069000Z\t1970-01-01T00:00:00.041Z\t1970-01-01T00:00:00.069Z\t1970-01-01T04:00:00.000000000Z\tb
                        271\t299\tO\tN\t171\t199\t235.5\t249.5\t377.5\t447.5\ta1\tc3\t71\t99\t71\t99\t1970-01-01T04:30:00.000000000Z\t1970-01-01T04:58:00.000000000Z\t1970-01-01T00:00:00.000071000Z\t1970-01-01T00:00:00.000099000Z\t1970-01-01T00:00:00.071Z\t1970-01-01T00:00:00.099Z\t1970-01-01T04:30:00.000000000Z\tb
                        """,
                "select first(i1) fi1, last(i1) li1, first(c1) fc1, " +
                        "last(c1) lc1, first(l1) fl1, last(l1) ll1, first(f1) ff1, last(f1) lf1, " +
                        "first(d1) fd1, last(d1) ld1, first(s1) fs1, last(s1) ls1, first(ss1) fss1, " +
                        "last(ss1) lss1, first(b1) fb1, last(b1) lb1, first(k) fk, last(k) lk, first(t1) ft1, " +
                        "last(t1) lt1, first(dt) fdt, last(dt) ldt, k, s\n" +
                        "from xx " +
                        "where s in ('b')" +
                        "sample by 30m align to first observation",
                """
                        insert into xx \
                        select \
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(200 * 60 * 1000000L, 1 * 60 * 1000000L) k,
                        cast(x + 200 as int) i1,\s
                        rnd_char() c1,\s
                        x+100 as l1,
                        cast(x * 0.5 + 200 as float) f1,\s
                        x*2.5 + 200 d1,
                        rnd_symbol(null, 'a1', 'b2', 'c3') s1,\s
                        cast(x as SHORT) ss1,
                        cast(x % 256 as byte) b1,
                        cast(x as timestamp) t1,
                        cast(x as date) dt
                        from
                        long_sequence(100)"""
        );
    }

    @Test
    public void testIndexSampleWithColumnTopsGeo() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table xx (s symbol, k timestamp_ns)" +
                    ", index(s capacity 256) timestamp(k) partition by DAY");

            execute(
                    """
                            insert into xx \
                            select \
                            (case when x % 2 = 0 then 'a' else 'b' end) s,
                            timestamp_sequence(0, 1 * 60 * 1000000L) k
                            from
                            long_sequence(100)
                            """);

            execute("alter table xx add i1 int", sqlExecutionContext);
            execute("alter table xx add c1 char", sqlExecutionContext);
            execute("alter table xx add l1 long", sqlExecutionContext);

            execute(
                    """
                            insert into xx \
                            select \
                            (case when x % 2 = 0 then 'a' else 'b' end) s,
                            timestamp_sequence(100 * 60 * 1000000L, 1 * 60 * 1000000L) k,
                            cast(x + 100 as int) i1,\s
                            rnd_char() c1,
                            x as l1
                            from
                            long_sequence(100)""");

            execute("alter table xx add f1 float", sqlExecutionContext);
            execute("alter table xx add d1 double", sqlExecutionContext);
            execute("alter table xx add s1 symbol", sqlExecutionContext);
            execute("alter table xx add ss1 short", sqlExecutionContext);
            execute("alter table xx add b1 byte", sqlExecutionContext);
            execute("alter table xx add t1 timestamp_ns", sqlExecutionContext);
            execute("alter table xx add dt date", sqlExecutionContext);
            execute("alter table xx add ge1 geohash(3b)", sqlExecutionContext);
            execute("alter table xx add ge2 geohash(2c)", sqlExecutionContext);
            execute("alter table xx add ge4 geohash(5c)", sqlExecutionContext);
            execute("alter table xx add ge8 geohash(9c)", sqlExecutionContext);
        });

        assertSampleByIndexQuery(
                """
                        fi1\tli1\tfc1\tlc1\tfl1\tll1\tff1\tlf1\tfd1\tld1\tfs1\tls1\tfss1\tlss1\tfb1\tlb1\tfk\tlk\tft1\tlt1\tfdt\tldt\tfge1\tlge1\tfge2\tlge2\tfge4\tlge4\tfge8\tlge8\tk\ts
                        null\tnull\t\t\tnull\tnull\tnull\tnull\tnull\tnull\t\t\t0\t0\t0\t0\t1970-01-01T00:00:00.000000000Z\t1970-01-01T00:28:00.000000000Z\t\t\t\t\t\t\t\t\t\t\t\t\t1970-01-01T00:00:00.000000000Z\tb
                        null\tnull\t\t\tnull\tnull\tnull\tnull\tnull\tnull\t\t\t0\t0\t0\t0\t1970-01-01T00:30:00.000000000Z\t1970-01-01T00:58:00.000000000Z\t\t\t\t\t\t\t\t\t\t\t\t\t1970-01-01T00:30:00.000000000Z\tb
                        null\tnull\t\t\tnull\tnull\tnull\tnull\tnull\tnull\t\t\t0\t0\t0\t0\t1970-01-01T01:00:00.000000000Z\t1970-01-01T01:28:00.000000000Z\t\t\t\t\t\t\t\t\t\t\t\t\t1970-01-01T01:00:00.000000000Z\tb
                        null\t119\t\tG\tnull\t19\tnull\tnull\tnull\tnull\t\t\t0\t0\t0\t0\t1970-01-01T01:30:00.000000000Z\t1970-01-01T01:58:00.000000000Z\t\t\t\t\t\t\t\t\t\t\t\t\t1970-01-01T01:30:00.000000000Z\tb
                        121\t149\tS\tL\t21\t49\tnull\tnull\tnull\tnull\t\t\t0\t0\t0\t0\t1970-01-01T02:00:00.000000000Z\t1970-01-01T02:28:00.000000000Z\t\t\t\t\t\t\t\t\t\t\t\t\t1970-01-01T02:00:00.000000000Z\tb
                        151\t179\tD\tR\t51\t79\tnull\tnull\tnull\tnull\t\t\t0\t0\t0\t0\t1970-01-01T02:30:00.000000000Z\t1970-01-01T02:58:00.000000000Z\t\t\t\t\t\t\t\t\t\t\t\t\t1970-01-01T02:30:00.000000000Z\tb
                        181\t209\tZ\tR\t81\t109\tnull\t204.5\tnull\t222.5\t\tb2\t0\t9\t0\t9\t1970-01-01T03:00:00.000000000Z\t1970-01-01T03:28:00.000000000Z\t\t1970-01-01T00:00:00.000009000Z\t\t1970-01-01T00:00:00.009Z\t\t010\t\tzy\t\tjzgum\t\t119gqw7wc\t1970-01-01T03:00:00.000000000Z\tb
                        211\t239\tU\tH\t111\t139\t205.5\t219.5\t227.5\t297.5\t\ta1\t11\t39\t11\t39\t1970-01-01T03:30:00.000000000Z\t1970-01-01T03:58:00.000000000Z\t1970-01-01T00:00:00.000011000Z\t1970-01-01T00:00:00.000039000Z\t1970-01-01T00:00:00.011Z\t1970-01-01T00:00:00.039Z\t010\t011\t8r\t01\t5dmtc\tpr5gy\tn54xrp1qg\t9tzcungsk\t1970-01-01T03:30:00.000000000Z\tb
                        241\t269\tJ\tR\t141\t169\t220.5\t234.5\t302.5\t372.5\t\tc3\t41\t69\t41\t69\t1970-01-01T04:00:00.000000000Z\t1970-01-01T04:28:00.000000000Z\t1970-01-01T00:00:00.000041000Z\t1970-01-01T00:00:00.000069000Z\t1970-01-01T00:00:00.041Z\t1970-01-01T00:00:00.069Z\t100\t001\tj8\tky\teuqer\twrv33\t791pjxsej\trzyp6xy6d\t1970-01-01T04:00:00.000000000Z\tb
                        271\t299\tR\tR\t171\t199\t235.5\t249.5\t377.5\t447.5\t\tc3\t71\t99\t71\t99\t1970-01-01T04:30:00.000000000Z\t1970-01-01T04:58:00.000000000Z\t1970-01-01T00:00:00.000071000Z\t1970-01-01T00:00:00.000099000Z\t1970-01-01T00:00:00.071Z\t1970-01-01T00:00:00.099Z\t001\t011\tby\tm1\t0rhez\t711s8\t57tv8npyb\t0prb8tpgj\t1970-01-01T04:30:00.000000000Z\tb
                        """,
                "select first(i1) fi1, last(i1) li1, first(c1) fc1, " +
                        "last(c1) lc1, first(l1) fl1, last(l1) ll1, first(f1) ff1, last(f1) lf1, " +
                        "first(d1) fd1, last(d1) ld1, first(s1) fs1, last(s1) ls1, first(ss1) fss1, " +
                        "last(ss1) lss1, first(b1) fb1, last(b1) lb1, first(k) fk, last(k) lk, first(t1) ft1, " +
                        "last(t1) lt1, first(dt) fdt, last(dt) ldt, " +
                        "first(ge1) fge1, last(ge1) lge1, first(ge2) fge2, last(ge2) lge2, first(ge4) fge4, last(ge4) lge4, first(ge8) fge8, last(ge8) lge8, k, s\n" +
                        "from xx " +
                        "where s in ('b')" +
                        "sample by 30m align to first observation",
                """
                        insert into xx \
                        select \
                        (case when x % 2 = 0 then 'a' else 'b' end) s,
                        timestamp_sequence(200 * 60 * 1000000L, 1 * 60 * 1000000L) k,
                        cast(x + 200 as int) i1,\s
                        rnd_char() c1,\s
                        x+100 as l1,
                        cast(x * 0.5 + 200 as float) f1,\s
                        x*2.5 + 200 d1,
                        rnd_symbol(null, 'a1', 'b2', 'c3') s1,\s
                        cast(x as SHORT) ss1,
                        cast(x % 256 as byte) b1,
                        cast(x as timestamp) t1,
                        cast(x as date) dt,
                        rnd_geohash(3) ge1,
                        rnd_geohash(10) ge2,
                        rnd_geohash(25) ge4,
                        rnd_geohash(45) ge8
                        from
                        long_sequence(100)"""
        );
    }

    @Test
    public void testIntervalAllVirtual() throws Exception {
        setCurrentMicros(MicrosTimestampDriver.floor("2023-01-01T11:22:33.000000000Z"));
        assertMemoryLeak(() -> assertSql(
                """
                        first\tcount\tts
                        ('2023-01-01T06:00:00.000Z', '2023-01-02T05:59:59.999Z')\t60\t2022-02-24T00:00:00.000000000Z
                        ('2023-01-01T06:00:00.000Z', '2023-01-02T05:59:59.999Z')\t60\t2022-02-24T01:00:00.000000000Z
                        ('2023-01-01T06:00:00.000Z', '2023-01-02T05:59:59.999Z')\t60\t2022-02-24T02:00:00.000000000Z
                        ('2023-01-01T06:00:00.000Z', '2023-01-02T05:59:59.999Z')\t60\t2022-02-24T03:00:00.000000000Z
                        ('2023-01-01T06:00:00.000Z', '2023-01-02T05:59:59.999Z')\t60\t2022-02-24T04:00:00.000000000Z
                        ('2023-01-01T06:00:00.000Z', '2023-01-02T05:59:59.999Z')\t60\t2022-02-24T05:00:00.000000000Z
                        ('2023-01-01T06:00:00.000Z', '2023-01-02T05:59:59.999Z')\t60\t2022-02-24T06:00:00.000000000Z
                        ('2023-01-01T06:00:00.000Z', '2023-01-02T05:59:59.999Z')\t60\t2022-02-24T07:00:00.000000000Z
                        ('2023-01-01T06:00:00.000Z', '2023-01-02T05:59:59.999Z')\t20\t2022-02-24T08:00:00.000000000Z
                        """,
                "select first(today), count(x), ts " +
                        "from ( " +
                        "  select today('UTC-06:00') today, x, timestamp_sequence_ns('2022-02-24', 60*1000*1000000) ts " +
                        "  from long_sequence(500) " +
                        ") timestamp(ts) " +
                        "SAMPLE by 1h;"
        ));
    }

    @Test
    public void testKeyedFromTo() throws Exception {
        assertException(
                "SELECT" +
                        "  day(ts) AS day, " +
                        "  sym2, " +
                        "  COUNT(*) AS c " +
                        "FROM x " +
                        "WHERE sym = 'abc' " +
                        "SAMPLE BY 1d FROM dateadd('d', -31, now()) to now() FILL(NULL);",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_symbol(5,4,4,1) sym," +
                        " rnd_symbol(5,4,4,1) sym2," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) ts" +
                        " from long_sequence(20)" +
                        ") timestamp(ts) partition by day",
                0,
                "FROM-TO intervals are not supported for keyed SAMPLE BY queries"
        );
    }

    @Test
    public void testNegativeOffsets() throws Exception {
        assertMemoryLeak(() -> assertSql("""
                        date\trow_count\ttotal_value
                        2023-12-31T11:55:00.000000000Z\t1\t100.0
                        2024-01-31T11:55:00.000000000Z\t1\t200.0
                        2024-02-29T11:55:00.000000000Z\t1\t300.0
                        2024-03-31T11:55:00.000000000Z\t1\t400.0
                        2024-04-30T11:55:00.000000000Z\t1\t500.0
                        2024-05-31T11:55:00.000000000Z\t1\t600.0
                        """,
                """
                        SELECT\s
                            date,
                            COUNT(*) AS row_count,
                            SUM(value) AS total_value
                        FROM (
                            SELECT cast('2024-01-15T12:00:00.000000Z' as timestamp_ns) as date, 100.0 as value FROM long_sequence(1)
                            UNION ALL
                            SELECT cast('2024-02-15T12:00:00.000000Z' as timestamp_ns), 200.0 FROM long_sequence(1)
                            UNION ALL
                            SELECT cast('2024-03-15T12:00:00.000000Z' as timestamp_ns), 300.0 FROM long_sequence(1)
                            UNION ALL
                            SELECT cast('2024-04-15T12:00:00.000000Z' as timestamp_ns), 400.0 FROM long_sequence(1)
                            UNION ALL
                            SELECT cast('2024-05-15T12:00:00.000000Z' as timestamp_ns), 500.0 FROM long_sequence(1)
                            UNION ALL
                            SELECT cast('2024-06-15T12:00:00.000000Z' as timestamp_ns), 600.0 FROM long_sequence(1)
                            ORDER BY date
                        )
                        SAMPLE BY 1M ALIGN TO CALENDAR WITH OFFSET '-12:05';"""
        ));
    }

    @Test
    public void testNoSampleByWithDeferredSingleSymbolFilterPageFrameRecordCursorFactory() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table xx (k timestamp_ns, d DOUBLE, s SYMBOL)" +
                    ", index(s capacity 345) timestamp(k) partition by DAY \n");

            execute("""
                    insert into xx \
                    select \
                    timestamp_sequence(25 * 60 * 60 * 1000000L, 1 * 60 * 1000000L),
                    rnd_double() d,
                    (case when x % 2 = 0 then 'a' else 'b' end) sk
                    from
                    long_sequence(300)
                    """);

            assertSql("""
                    sum
                    75.42541658721542
                    """, "select sum(d)\n" +
                    "from xx " +
                    "where s in ('a')"
            );
        });
    }

    @Test
    public void testRedundantGroupByInKeyedFromTo1() throws Exception {
        assertException(
                "SELECT" +
                        "  day(ts) AS day, " +
                        "  sym2, " +
                        "  COUNT(*) AS c " +
                        "FROM x " +
                        "WHERE sym = 'abc' " +
                        "SAMPLE BY 1d " +
                        "GROUP BY day, sym2 ",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_symbol(5,4,4,1) sym," +
                        " rnd_symbol(5,4,4,1) sym2," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) ts" +
                        " from long_sequence(20)" +
                        ") timestamp(ts) partition by day",
                95,
                "SELECT query must not contain both GROUP BY and SAMPLE BY"
        );
    }

    @Test
    public void testRedundantGroupByInKeyedFromTo2() throws Exception {
        assertException(
                "SELECT" +
                        "  day(ts) AS day, " +
                        "  sym2, " +
                        "  COUNT(*) AS c " +
                        "FROM x " +
                        "WHERE sym = 'abc' " +
                        "SAMPLE BY 1d FROM dateadd('d', -31, now()) to now() FILL(NULL) " +
                        "GROUP BY day, sym2 " +
                        "ORDER BY day(ts) DESC, sym2;",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_symbol(5,4,4,1) sym," +
                        " rnd_symbol(5,4,4,1) sym2," +
                        " timestamp_sequence_ns(172800000000, 3600000000000) ts" +
                        " from long_sequence(20)" +
                        ") timestamp(ts) partition by day",
                145,
                "SELECT query must not contain both GROUP BY and SAMPLE BY"
        );
    }

    @Test
    public void testSampleBadFunction() throws Exception {
        String stringType = ColumnType.nameOf(ColumnType.STRING);
        assertException(
                "select b, sumx(a, 'ab') k from x sample by 3h fill(none)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                0,
                "inconvertible value: `ab` [" + stringType + " -> DOUBLE]"
        );
    }

    @Test
    public void testSampleBadFunctionInterpolated() throws Exception {
        String stringType = ColumnType.nameOf(ColumnType.STRING);
        assertException(
                "select b, sumx(a, 'ac') k from x sample by 3h fill(linear)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                0,
                "inconvertible value: `ac` [" + stringType + " -> DOUBLE]"
        );
    }

    @Test
    public void testSampleByAlignToCalendarDSTGapNoBackwardJumps() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table x (i int, ts timestamp_ns) timestamp(ts) PARTITION by day;");
            execute(
                    "insert into x " +
                            "select x, timestamp_sequence('2021-03-26', 60 * 1000000) " +
                            "from long_sequence(4 * 24 * 60);"
            );

            // 17m
            assertQueryNoLeakCheck(
                    """
                            min\tmax\tts
                            2821\t2822\t2021-03-27T22:45:00.000000000Z
                            2823\t2839\t2021-03-27T23:02:00.000000000Z
                            2840\t2856\t2021-03-27T23:19:00.000000000Z
                            2857\t2873\t2021-03-27T23:36:00.000000000Z
                            2874\t2890\t2021-03-27T23:53:00.000000000Z
                            2891\t2907\t2021-03-28T00:10:00.000000000Z
                            2908\t2924\t2021-03-28T00:27:00.000000000Z
                            2925\t2949\t2021-03-28T00:44:00.000000000Z
                            2950\t2966\t2021-03-28T01:09:00.000000000Z
                            2967\t2983\t2021-03-28T01:26:00.000000000Z
                            """,
                    "select min(i), max(i), ts from x " +
                            "where ts between '2021-03-27T23:00:00.000000000Z' and '2021-03-28T01:42:59.999999Z' " +
                            "sample by 17m align to calendar time zone 'Europe/Berlin';",
                    "ts",
                    true,
                    true
            );

            // The timestamps in the second query must not be before the ones from the first query.
            // If we wouldn't be doing DST gap hour check when flooring the timestamps, that would not hold.
            assertQueryNoLeakCheck(
                    """
                            min\tmax\tts
                            2984\t3000\t2021-03-28T01:43:00.000000000Z
                            3001\t3017\t2021-03-28T02:00:00.000000000Z
                            3018\t3034\t2021-03-28T02:17:00.000000000Z
                            3035\t3051\t2021-03-28T02:34:00.000000000Z
                            3052\t3068\t2021-03-28T02:51:00.000000000Z
                            3069\t3085\t2021-03-28T03:08:00.000000000Z
                            3086\t3102\t2021-03-28T03:25:00.000000000Z
                            3103\t3119\t2021-03-28T03:42:00.000000000Z
                            3120\t3136\t2021-03-28T03:59:00.000000000Z
                            3137\t3153\t2021-03-28T04:16:00.000000000Z
                            3154\t3170\t2021-03-28T04:33:00.000000000Z
                            3171\t3187\t2021-03-28T04:50:00.000000000Z
                            3188\t3204\t2021-03-28T05:07:00.000000000Z
                            3205\t3221\t2021-03-28T05:24:00.000000000Z
                            3222\t3238\t2021-03-28T05:41:00.000000000Z
                            3239\t3255\t2021-03-28T05:58:00.000000000Z
                            3256\t3272\t2021-03-28T06:15:00.000000000Z
                            3273\t3289\t2021-03-28T06:32:00.000000000Z
                            3290\t3306\t2021-03-28T06:49:00.000000000Z
                            3307\t3323\t2021-03-28T07:06:00.000000000Z
                            3324\t3340\t2021-03-28T07:23:00.000000000Z
                            3341\t3357\t2021-03-28T07:40:00.000000000Z
                            3358\t3374\t2021-03-28T07:57:00.000000000Z
                            3375\t3391\t2021-03-28T08:14:00.000000000Z
                            3392\t3408\t2021-03-28T08:31:00.000000000Z
                            3409\t3425\t2021-03-28T08:48:00.000000000Z
                            3426\t3442\t2021-03-28T09:05:00.000000000Z
                            3443\t3459\t2021-03-28T09:22:00.000000000Z
                            3460\t3476\t2021-03-28T09:39:00.000000000Z
                            3477\t3493\t2021-03-28T09:56:00.000000000Z
                            3494\t3510\t2021-03-28T10:13:00.000000000Z
                            3511\t3527\t2021-03-28T10:30:00.000000000Z
                            3528\t3544\t2021-03-28T10:47:00.000000000Z
                            3545\t3561\t2021-03-28T11:04:00.000000000Z
                            3562\t3578\t2021-03-28T11:21:00.000000000Z
                            3579\t3595\t2021-03-28T11:38:00.000000000Z
                            3596\t3596\t2021-03-28T11:55:00.000000000Z
                            """,
                    "select min(i), max(i), ts from x " +
                            "where ts between '2021-03-28T01:43:00.000000000Z' and '2021-03-28T11:55:00.000000000Z' " +
                            "sample by 17m align to calendar time zone 'Europe/Berlin';",
                    "ts",
                    true,
                    true
            );

            // 5m
            assertQueryNoLeakCheck(
                    """
                            min\tmax\tts
                            2821\t2825\t2021-03-27T23:00:00.000000000Z
                            2826\t2830\t2021-03-27T23:05:00.000000000Z
                            2831\t2835\t2021-03-27T23:10:00.000000000Z
                            2836\t2840\t2021-03-27T23:15:00.000000000Z
                            2841\t2845\t2021-03-27T23:20:00.000000000Z
                            2846\t2850\t2021-03-27T23:25:00.000000000Z
                            2851\t2855\t2021-03-27T23:30:00.000000000Z
                            2856\t2860\t2021-03-27T23:35:00.000000000Z
                            2861\t2865\t2021-03-27T23:40:00.000000000Z
                            2866\t2870\t2021-03-27T23:45:00.000000000Z
                            2871\t2875\t2021-03-27T23:50:00.000000000Z
                            2876\t2880\t2021-03-27T23:55:00.000000000Z
                            2881\t2885\t2021-03-28T00:00:00.000000000Z
                            2886\t2890\t2021-03-28T00:05:00.000000000Z
                            2891\t2895\t2021-03-28T00:10:00.000000000Z
                            2896\t2900\t2021-03-28T00:15:00.000000000Z
                            2901\t2905\t2021-03-28T00:20:00.000000000Z
                            2906\t2910\t2021-03-28T00:25:00.000000000Z
                            2911\t2915\t2021-03-28T00:30:00.000000000Z
                            2916\t2920\t2021-03-28T00:35:00.000000000Z
                            2921\t2925\t2021-03-28T00:40:00.000000000Z
                            2926\t2930\t2021-03-28T00:45:00.000000000Z
                            2931\t2935\t2021-03-28T00:50:00.000000000Z
                            2936\t2940\t2021-03-28T00:55:00.000000000Z
                            2941\t2945\t2021-03-28T01:00:00.000000000Z
                            2946\t2950\t2021-03-28T01:05:00.000000000Z
                            2951\t2955\t2021-03-28T01:10:00.000000000Z
                            2956\t2960\t2021-03-28T01:15:00.000000000Z
                            2961\t2965\t2021-03-28T01:20:00.000000000Z
                            2966\t2970\t2021-03-28T01:25:00.000000000Z
                            2971\t2975\t2021-03-28T01:30:00.000000000Z
                            2976\t2980\t2021-03-28T01:35:00.000000000Z
                            2981\t2983\t2021-03-28T01:40:00.000000000Z
                            """,
                    "select min(i), max(i), ts from x " +
                            "where ts between '2021-03-27T23:00:00.000000000Z' and '2021-03-28T01:42:59.999999Z' " +
                            "sample by 5m align to calendar time zone 'Europe/Berlin';",
                    "ts",
                    true,
                    true
            );

            assertQueryNoLeakCheck(
                    """
                            min\tmax\tts
                            2984\t2985\t2021-03-28T01:40:00.000000000Z
                            2986\t2990\t2021-03-28T01:45:00.000000000Z
                            2991\t2995\t2021-03-28T01:50:00.000000000Z
                            2996\t3000\t2021-03-28T01:55:00.000000000Z
                            3001\t3005\t2021-03-28T02:00:00.000000000Z
                            3006\t3010\t2021-03-28T02:05:00.000000000Z
                            3011\t3015\t2021-03-28T02:10:00.000000000Z
                            3016\t3020\t2021-03-28T02:15:00.000000000Z
                            3021\t3025\t2021-03-28T02:20:00.000000000Z
                            3026\t3030\t2021-03-28T02:25:00.000000000Z
                            3031\t3035\t2021-03-28T02:30:00.000000000Z
                            3036\t3040\t2021-03-28T02:35:00.000000000Z
                            3041\t3045\t2021-03-28T02:40:00.000000000Z
                            3046\t3050\t2021-03-28T02:45:00.000000000Z
                            3051\t3055\t2021-03-28T02:50:00.000000000Z
                            3056\t3060\t2021-03-28T02:55:00.000000000Z
                            3061\t3065\t2021-03-28T03:00:00.000000000Z
                            3066\t3070\t2021-03-28T03:05:00.000000000Z
                            3071\t3075\t2021-03-28T03:10:00.000000000Z
                            3076\t3080\t2021-03-28T03:15:00.000000000Z
                            3081\t3085\t2021-03-28T03:20:00.000000000Z
                            3086\t3090\t2021-03-28T03:25:00.000000000Z
                            3091\t3095\t2021-03-28T03:30:00.000000000Z
                            3096\t3100\t2021-03-28T03:35:00.000000000Z
                            3101\t3105\t2021-03-28T03:40:00.000000000Z
                            3106\t3110\t2021-03-28T03:45:00.000000000Z
                            3111\t3115\t2021-03-28T03:50:00.000000000Z
                            3116\t3120\t2021-03-28T03:55:00.000000000Z
                            3121\t3125\t2021-03-28T04:00:00.000000000Z
                            3126\t3130\t2021-03-28T04:05:00.000000000Z
                            3131\t3135\t2021-03-28T04:10:00.000000000Z
                            3136\t3140\t2021-03-28T04:15:00.000000000Z
                            3141\t3145\t2021-03-28T04:20:00.000000000Z
                            3146\t3150\t2021-03-28T04:25:00.000000000Z
                            3151\t3155\t2021-03-28T04:30:00.000000000Z
                            3156\t3160\t2021-03-28T04:35:00.000000000Z
                            3161\t3165\t2021-03-28T04:40:00.000000000Z
                            3166\t3170\t2021-03-28T04:45:00.000000000Z
                            3171\t3175\t2021-03-28T04:50:00.000000000Z
                            3176\t3180\t2021-03-28T04:55:00.000000000Z
                            3181\t3185\t2021-03-28T05:00:00.000000000Z
                            3186\t3190\t2021-03-28T05:05:00.000000000Z
                            3191\t3195\t2021-03-28T05:10:00.000000000Z
                            3196\t3200\t2021-03-28T05:15:00.000000000Z
                            3201\t3205\t2021-03-28T05:20:00.000000000Z
                            3206\t3210\t2021-03-28T05:25:00.000000000Z
                            3211\t3215\t2021-03-28T05:30:00.000000000Z
                            3216\t3220\t2021-03-28T05:35:00.000000000Z
                            3221\t3225\t2021-03-28T05:40:00.000000000Z
                            3226\t3230\t2021-03-28T05:45:00.000000000Z
                            3231\t3235\t2021-03-28T05:50:00.000000000Z
                            3236\t3240\t2021-03-28T05:55:00.000000000Z
                            3241\t3245\t2021-03-28T06:00:00.000000000Z
                            3246\t3250\t2021-03-28T06:05:00.000000000Z
                            3251\t3255\t2021-03-28T06:10:00.000000000Z
                            3256\t3260\t2021-03-28T06:15:00.000000000Z
                            3261\t3265\t2021-03-28T06:20:00.000000000Z
                            3266\t3270\t2021-03-28T06:25:00.000000000Z
                            3271\t3275\t2021-03-28T06:30:00.000000000Z
                            3276\t3280\t2021-03-28T06:35:00.000000000Z
                            3281\t3285\t2021-03-28T06:40:00.000000000Z
                            3286\t3290\t2021-03-28T06:45:00.000000000Z
                            3291\t3295\t2021-03-28T06:50:00.000000000Z
                            3296\t3300\t2021-03-28T06:55:00.000000000Z
                            3301\t3305\t2021-03-28T07:00:00.000000000Z
                            3306\t3310\t2021-03-28T07:05:00.000000000Z
                            3311\t3315\t2021-03-28T07:10:00.000000000Z
                            3316\t3320\t2021-03-28T07:15:00.000000000Z
                            3321\t3325\t2021-03-28T07:20:00.000000000Z
                            3326\t3330\t2021-03-28T07:25:00.000000000Z
                            3331\t3335\t2021-03-28T07:30:00.000000000Z
                            3336\t3340\t2021-03-28T07:35:00.000000000Z
                            3341\t3345\t2021-03-28T07:40:00.000000000Z
                            3346\t3350\t2021-03-28T07:45:00.000000000Z
                            3351\t3355\t2021-03-28T07:50:00.000000000Z
                            3356\t3360\t2021-03-28T07:55:00.000000000Z
                            3361\t3365\t2021-03-28T08:00:00.000000000Z
                            3366\t3370\t2021-03-28T08:05:00.000000000Z
                            3371\t3375\t2021-03-28T08:10:00.000000000Z
                            3376\t3380\t2021-03-28T08:15:00.000000000Z
                            3381\t3385\t2021-03-28T08:20:00.000000000Z
                            3386\t3390\t2021-03-28T08:25:00.000000000Z
                            3391\t3395\t2021-03-28T08:30:00.000000000Z
                            3396\t3400\t2021-03-28T08:35:00.000000000Z
                            3401\t3405\t2021-03-28T08:40:00.000000000Z
                            3406\t3410\t2021-03-28T08:45:00.000000000Z
                            3411\t3415\t2021-03-28T08:50:00.000000000Z
                            3416\t3420\t2021-03-28T08:55:00.000000000Z
                            3421\t3425\t2021-03-28T09:00:00.000000000Z
                            3426\t3430\t2021-03-28T09:05:00.000000000Z
                            3431\t3435\t2021-03-28T09:10:00.000000000Z
                            3436\t3440\t2021-03-28T09:15:00.000000000Z
                            3441\t3445\t2021-03-28T09:20:00.000000000Z
                            3446\t3450\t2021-03-28T09:25:00.000000000Z
                            3451\t3455\t2021-03-28T09:30:00.000000000Z
                            3456\t3460\t2021-03-28T09:35:00.000000000Z
                            3461\t3465\t2021-03-28T09:40:00.000000000Z
                            3466\t3470\t2021-03-28T09:45:00.000000000Z
                            3471\t3475\t2021-03-28T09:50:00.000000000Z
                            3476\t3480\t2021-03-28T09:55:00.000000000Z
                            3481\t3485\t2021-03-28T10:00:00.000000000Z
                            3486\t3490\t2021-03-28T10:05:00.000000000Z
                            3491\t3495\t2021-03-28T10:10:00.000000000Z
                            3496\t3500\t2021-03-28T10:15:00.000000000Z
                            3501\t3505\t2021-03-28T10:20:00.000000000Z
                            3506\t3510\t2021-03-28T10:25:00.000000000Z
                            3511\t3515\t2021-03-28T10:30:00.000000000Z
                            3516\t3520\t2021-03-28T10:35:00.000000000Z
                            3521\t3525\t2021-03-28T10:40:00.000000000Z
                            3526\t3530\t2021-03-28T10:45:00.000000000Z
                            3531\t3535\t2021-03-28T10:50:00.000000000Z
                            3536\t3540\t2021-03-28T10:55:00.000000000Z
                            3541\t3545\t2021-03-28T11:00:00.000000000Z
                            3546\t3550\t2021-03-28T11:05:00.000000000Z
                            3551\t3555\t2021-03-28T11:10:00.000000000Z
                            3556\t3560\t2021-03-28T11:15:00.000000000Z
                            3561\t3565\t2021-03-28T11:20:00.000000000Z
                            3566\t3570\t2021-03-28T11:25:00.000000000Z
                            3571\t3575\t2021-03-28T11:30:00.000000000Z
                            3576\t3580\t2021-03-28T11:35:00.000000000Z
                            3581\t3585\t2021-03-28T11:40:00.000000000Z
                            3586\t3590\t2021-03-28T11:45:00.000000000Z
                            3591\t3595\t2021-03-28T11:50:00.000000000Z
                            3596\t3596\t2021-03-28T11:55:00.000000000Z
                            """,
                    "select min(i), max(i), ts from x " +
                            "where ts between '2021-03-28T01:43:00.000000000Z' and '2021-03-28T11:55:00.000000000Z' " +
                            "sample by 5m align to calendar time zone 'Europe/Berlin';",
                    "ts",
                    true,
                    true
            );

            // 15m
            assertQueryNoLeakCheck(
                    """
                            min\tmax\tts
                            2821\t2835\t2021-03-27T23:00:00.000000000Z
                            2836\t2850\t2021-03-27T23:15:00.000000000Z
                            2851\t2865\t2021-03-27T23:30:00.000000000Z
                            2866\t2880\t2021-03-27T23:45:00.000000000Z
                            2881\t2895\t2021-03-28T00:00:00.000000000Z
                            2896\t2910\t2021-03-28T00:15:00.000000000Z
                            2911\t2925\t2021-03-28T00:30:00.000000000Z
                            2926\t2940\t2021-03-28T00:45:00.000000000Z
                            2941\t2955\t2021-03-28T01:00:00.000000000Z
                            2956\t2970\t2021-03-28T01:15:00.000000000Z
                            2971\t2983\t2021-03-28T01:30:00.000000000Z
                            """,
                    "select min(i), max(i), ts from x " +
                            "where ts between '2021-03-27T23:00:00.000000000Z' and '2021-03-28T01:42:59.999999Z' " +
                            "sample by 15m align to calendar time zone 'Europe/Berlin';",
                    "ts",
                    true,
                    true
            );

            assertQueryNoLeakCheck(
                    """
                            min\tmax\tts
                            2984\t2985\t2021-03-28T01:30:00.000000000Z
                            2986\t3000\t2021-03-28T01:45:00.000000000Z
                            3001\t3015\t2021-03-28T02:00:00.000000000Z
                            3016\t3030\t2021-03-28T02:15:00.000000000Z
                            3031\t3045\t2021-03-28T02:30:00.000000000Z
                            3046\t3060\t2021-03-28T02:45:00.000000000Z
                            3061\t3075\t2021-03-28T03:00:00.000000000Z
                            3076\t3090\t2021-03-28T03:15:00.000000000Z
                            3091\t3105\t2021-03-28T03:30:00.000000000Z
                            3106\t3120\t2021-03-28T03:45:00.000000000Z
                            3121\t3135\t2021-03-28T04:00:00.000000000Z
                            3136\t3150\t2021-03-28T04:15:00.000000000Z
                            3151\t3165\t2021-03-28T04:30:00.000000000Z
                            3166\t3180\t2021-03-28T04:45:00.000000000Z
                            3181\t3195\t2021-03-28T05:00:00.000000000Z
                            3196\t3210\t2021-03-28T05:15:00.000000000Z
                            3211\t3225\t2021-03-28T05:30:00.000000000Z
                            3226\t3240\t2021-03-28T05:45:00.000000000Z
                            3241\t3255\t2021-03-28T06:00:00.000000000Z
                            3256\t3270\t2021-03-28T06:15:00.000000000Z
                            3271\t3285\t2021-03-28T06:30:00.000000000Z
                            3286\t3300\t2021-03-28T06:45:00.000000000Z
                            3301\t3315\t2021-03-28T07:00:00.000000000Z
                            3316\t3330\t2021-03-28T07:15:00.000000000Z
                            3331\t3345\t2021-03-28T07:30:00.000000000Z
                            3346\t3360\t2021-03-28T07:45:00.000000000Z
                            3361\t3375\t2021-03-28T08:00:00.000000000Z
                            3376\t3390\t2021-03-28T08:15:00.000000000Z
                            3391\t3405\t2021-03-28T08:30:00.000000000Z
                            3406\t3420\t2021-03-28T08:45:00.000000000Z
                            3421\t3435\t2021-03-28T09:00:00.000000000Z
                            3436\t3450\t2021-03-28T09:15:00.000000000Z
                            3451\t3465\t2021-03-28T09:30:00.000000000Z
                            3466\t3480\t2021-03-28T09:45:00.000000000Z
                            3481\t3495\t2021-03-28T10:00:00.000000000Z
                            3496\t3510\t2021-03-28T10:15:00.000000000Z
                            3511\t3525\t2021-03-28T10:30:00.000000000Z
                            3526\t3540\t2021-03-28T10:45:00.000000000Z
                            3541\t3555\t2021-03-28T11:00:00.000000000Z
                            3556\t3570\t2021-03-28T11:15:00.000000000Z
                            3571\t3585\t2021-03-28T11:30:00.000000000Z
                            3586\t3596\t2021-03-28T11:45:00.000000000Z
                            """,
                    "select min(i), max(i), ts from x " +
                            "where ts between '2021-03-28T01:43:00.000000000Z' and '2021-03-28T11:55:00.000000000Z' " +
                            "sample by 15m align to calendar time zone 'Europe/Berlin';",
                    "ts",
                    true,
                    true
            );

            // 30m
            assertQueryNoLeakCheck(
                    """
                            min\tmax\tts
                            2821\t2850\t2021-03-27T23:00:00.000000000Z
                            2851\t2880\t2021-03-27T23:30:00.000000000Z
                            2881\t2910\t2021-03-28T00:00:00.000000000Z
                            2911\t2940\t2021-03-28T00:30:00.000000000Z
                            2941\t2970\t2021-03-28T01:00:00.000000000Z
                            2971\t2983\t2021-03-28T01:30:00.000000000Z
                            """,
                    "select min(i), max(i), ts from x " +
                            "where ts between '2021-03-27T23:00:00.000000000Z' and '2021-03-28T01:42:59.999999Z' " +
                            "sample by 30m align to calendar time zone 'Europe/Berlin';",
                    "ts",
                    true,
                    true
            );

            assertQueryNoLeakCheck(
                    """
                            min\tmax\tts
                            2984\t3000\t2021-03-28T01:30:00.000000000Z
                            3001\t3030\t2021-03-28T02:00:00.000000000Z
                            3031\t3060\t2021-03-28T02:30:00.000000000Z
                            3061\t3090\t2021-03-28T03:00:00.000000000Z
                            3091\t3120\t2021-03-28T03:30:00.000000000Z
                            3121\t3150\t2021-03-28T04:00:00.000000000Z
                            3151\t3180\t2021-03-28T04:30:00.000000000Z
                            3181\t3210\t2021-03-28T05:00:00.000000000Z
                            3211\t3240\t2021-03-28T05:30:00.000000000Z
                            3241\t3270\t2021-03-28T06:00:00.000000000Z
                            3271\t3300\t2021-03-28T06:30:00.000000000Z
                            3301\t3330\t2021-03-28T07:00:00.000000000Z
                            3331\t3360\t2021-03-28T07:30:00.000000000Z
                            3361\t3390\t2021-03-28T08:00:00.000000000Z
                            3391\t3420\t2021-03-28T08:30:00.000000000Z
                            3421\t3450\t2021-03-28T09:00:00.000000000Z
                            3451\t3480\t2021-03-28T09:30:00.000000000Z
                            3481\t3510\t2021-03-28T10:00:00.000000000Z
                            3511\t3540\t2021-03-28T10:30:00.000000000Z
                            3541\t3570\t2021-03-28T11:00:00.000000000Z
                            3571\t3596\t2021-03-28T11:30:00.000000000Z
                            """,
                    "select min(i), max(i), ts from x " +
                            "where ts between '2021-03-28T01:43:00.000000000Z' and '2021-03-28T11:55:00.000000000Z' " +
                            "sample by 30m align to calendar time zone 'Europe/Berlin';",
                    "ts",
                    true,
                    true
            );
        });
    }

    @Test
    public void testSampleByAlignToCalendarDSTGapNoBackwardJumpsWithFilter() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table x (i int, ts timestamp_ns) timestamp(ts) PARTITION by day;");
            execute(
                    "insert into x " +
                            "select x, timestamp_sequence('2021-03-26', 60 * 1000000) " +
                            "from long_sequence(4 * 24 * 60);"
            );

            assertQueryNoLeakCheck(
                    """
                            min\tmax\tcount\tts
                            2926\t2952\t27\t2021-03-28T00:45:00.000000000Z
                            2953\t2981\t29\t2021-03-28T01:12:00.000000000Z
                            2982\t3010\t29\t2021-03-28T01:41:00.000000000Z
                            3011\t3039\t29\t2021-03-28T02:10:00.000000000Z
                            3040\t3068\t29\t2021-03-28T02:39:00.000000000Z
                            3069\t3097\t29\t2021-03-28T03:08:00.000000000Z
                            3098\t3126\t29\t2021-03-28T03:37:00.000000000Z
                            3127\t3155\t29\t2021-03-28T04:06:00.000000000Z
                            3156\t3184\t29\t2021-03-28T04:35:00.000000000Z
                            3185\t3213\t29\t2021-03-28T05:04:00.000000000Z
                            3214\t3242\t29\t2021-03-28T05:33:00.000000000Z
                            3243\t3271\t29\t2021-03-28T06:02:00.000000000Z
                            3272\t3300\t29\t2021-03-28T06:31:00.000000000Z
                            """,
                    "select min(i), max(i), count(), ts from  x " +
                            "where ts between '2021-03-28T00:45:00.000000000Z' and '2021-03-28T06:59:59.999999Z' " +
                            "sample by 29m align to calendar time zone 'Europe/Berlin' " +
                            "with offset '00:15';",
                    "ts",
                    true,
                    true
            );

            assertQueryNoLeakCheck(
                    """
                            min\tmax\tcount\tts
                            2718\t3163\t446\t2021-03-27T21:17:00.000000000Z
                            """,
                    "select min(i), max(i), count(), ts from  x " +
                            "where ts between '2021-03-27T21:17:00' and '2021-03-28T04:42:59.999999' " +
                            "sample by 253m align to calendar time zone 'Europe/Berlin' " +
                            "with offset '00:15';",
                    "ts",
                    true,
                    true
            );
        });
    }

    @Test
    public void testSampleByAlignToCalendarDSTGapNoBackwardJumpsWithFrom() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table x (i int, ts timestamp_ns) timestamp(ts) PARTITION by day;");
            execute(
                    "insert into x " +
                            "select x, timestamp_sequence('2021-03-26', 60 * 1000000) " +
                            "from long_sequence(4 * 24 * 60);"
            );

            assertQueryNoLeakCheck(
                    """
                            min\tmax\tts
                            2821\t2825\t2021-03-27T22:48:00.000000000Z
                            2826\t2842\t2021-03-27T23:05:00.000000000Z
                            2843\t2859\t2021-03-27T23:22:00.000000000Z
                            2860\t2876\t2021-03-27T23:39:00.000000000Z
                            2877\t2893\t2021-03-27T23:56:00.000000000Z
                            2894\t2910\t2021-03-28T00:13:00.000000000Z
                            2911\t2927\t2021-03-28T00:30:00.000000000Z
                            2928\t2952\t2021-03-28T00:47:00.000000000Z
                            2953\t2969\t2021-03-28T01:12:00.000000000Z
                            2970\t2983\t2021-03-28T01:29:00.000000000Z
                            """,
                    "select min(i), max(i), ts from x " +
                            "where ts between '2021-03-27T23:00:00.000000000Z' and '2021-03-28T01:42:59.999999Z' " +
                            "sample by 17m from '2021-03-27' align to calendar time zone 'Europe/Berlin';",
                    "ts",
                    true,
                    true
            );

            // The timestamps in the second query must not be before the ones from the first query.
            // If we wouldn't be doing DST gap hour check when flooring the timestamps, that would not hold.
            assertQueryNoLeakCheck(
                    """
                            min\tmax\tts
                            2984\t2998\t2021-03-28T01:41:00.000000000Z
                            2999\t3015\t2021-03-28T01:58:00.000000000Z
                            3016\t3032\t2021-03-28T02:15:00.000000000Z
                            3033\t3049\t2021-03-28T02:32:00.000000000Z
                            3050\t3066\t2021-03-28T02:49:00.000000000Z
                            3067\t3083\t2021-03-28T03:06:00.000000000Z
                            3084\t3100\t2021-03-28T03:23:00.000000000Z
                            3101\t3117\t2021-03-28T03:40:00.000000000Z
                            3118\t3134\t2021-03-28T03:57:00.000000000Z
                            3135\t3151\t2021-03-28T04:14:00.000000000Z
                            3152\t3168\t2021-03-28T04:31:00.000000000Z
                            3169\t3185\t2021-03-28T04:48:00.000000000Z
                            3186\t3202\t2021-03-28T05:05:00.000000000Z
                            3203\t3219\t2021-03-28T05:22:00.000000000Z
                            3220\t3236\t2021-03-28T05:39:00.000000000Z
                            3237\t3253\t2021-03-28T05:56:00.000000000Z
                            3254\t3270\t2021-03-28T06:13:00.000000000Z
                            3271\t3287\t2021-03-28T06:30:00.000000000Z
                            3288\t3304\t2021-03-28T06:47:00.000000000Z
                            3305\t3321\t2021-03-28T07:04:00.000000000Z
                            3322\t3338\t2021-03-28T07:21:00.000000000Z
                            3339\t3355\t2021-03-28T07:38:00.000000000Z
                            3356\t3372\t2021-03-28T07:55:00.000000000Z
                            3373\t3389\t2021-03-28T08:12:00.000000000Z
                            3390\t3406\t2021-03-28T08:29:00.000000000Z
                            3407\t3423\t2021-03-28T08:46:00.000000000Z
                            3424\t3440\t2021-03-28T09:03:00.000000000Z
                            3441\t3457\t2021-03-28T09:20:00.000000000Z
                            3458\t3474\t2021-03-28T09:37:00.000000000Z
                            3475\t3491\t2021-03-28T09:54:00.000000000Z
                            3492\t3508\t2021-03-28T10:11:00.000000000Z
                            3509\t3525\t2021-03-28T10:28:00.000000000Z
                            3526\t3542\t2021-03-28T10:45:00.000000000Z
                            3543\t3559\t2021-03-28T11:02:00.000000000Z
                            3560\t3576\t2021-03-28T11:19:00.000000000Z
                            3577\t3593\t2021-03-28T11:36:00.000000000Z
                            3594\t3596\t2021-03-28T11:53:00.000000000Z
                            """,
                    "select min(i), max(i), ts from x " +
                            "where ts between '2021-03-28T01:43:00.000000000Z' and '2021-03-28T11:55:00.000000000Z' " +
                            "sample by 17m from '2021-03-28' align to calendar time zone 'Europe/Berlin';",
                    "ts",
                    true,
                    true
            );
        });
    }

    @Test
    public void testSampleByAlignToCalendarFillNoneWithoutKey1() throws Exception {
        assertQuery(
                """
                        ts\tfirst\tavg\tlast\tmax
                        2022-12-01T00:01:30.000000000Z\t3\t3.0\t3\t3
                        """,
                "select * from (" +
                        "select ts, first(val), avg(val), last(val), max(val)" +
                        "from x " +
                        "sample by 1m align to first observation)" +
                        "where ts > '2022-12-01T00:00:30.000000000Z' ",
                "create table x as " +
                        "(" +
                        "select '2022-12-01T00:00:30.000000000Z'::timestamp_ns as ts, 1 as val union all " +
                        "select '2022-12-01T00:00:35.000000000Z'::timestamp_ns, 2 union all " +
                        "select '2022-12-01T00:01:31.000000000Z'::timestamp_ns, 3 from long_sequence(1)  " +
                        ") timestamp(ts) partition by DAY",
                "ts",
                false
        );
    }

    @Test
    public void testSampleByAlignToCalendarFillNoneWithoutKey2() throws Exception {
        assertQuery(
                """
                        ts\tfirst\tavg\tlast\tmax
                        2022-12-01T00:00:30.000000000Z\t1\t1.5\t2\t2
                        2022-12-01T00:01:30.000000000Z\t3\t3.0\t3\t3
                        """,
                "select * from (" +
                        "select ts, first(val), avg(val), last(val), max(val)" +
                        "from x " +
                        "sample by 1m align to first observation)" +
                        "where ts < '2022-12-01T00:01:31.000000000Z' ",
                "create table x as " +
                        "(" +
                        "select '2022-12-01T00:00:30.000000000Z'::timestamp_ns as ts, 1 as val union all " +
                        "select '2022-12-01T00:00:35.000000000Z'::timestamp_ns, 2 union all " +
                        "select '2022-12-01T00:01:31.000000000Z'::timestamp_ns, 3 from long_sequence(1)  " +
                        ") timestamp(ts) partition by DAY",
                "ts",
                false
        );
    }

    @Test
    public void testSampleByAlignToCalendarFillNullWithKey1() throws Exception {
        assertQuery(
                """
                        ts\ts\tfirst\tavg\tlast\tmax
                        2022-12-01T00:00:00.000000000Z\ts2\tnull\tnull\tnull\tnull
                        2022-12-01T00:01:00.000000000Z\ts2\t2\t2.0\t2\t2
                        2022-12-01T00:02:00.000000000Z\ts2\t3\t3.0\t3\t3
                        """,
                "select * from (" +
                        "select ts, s, first(val), avg(val), last(val), max(val)" +
                        "from x " +
                        "sample by 1m fill(null) align to calendar  )" +
                        "where s != 's1' ",
                "create table x as " +
                        "(" +
                        "select '2022-12-01T00:00:30.000000000Z'::timestamp_ns as ts, 's1' as s, 1 as val union all " +
                        "select '2022-12-01T00:00:35.000000000Z'::timestamp_ns, 's1', 2 union all " +
                        "select '2022-12-01T00:01:36.000000000Z'::timestamp_ns, 's2', 2 union all " +
                        "select '2022-12-01T00:02:31.000000000Z'::timestamp_ns, 's2', 3 from long_sequence(1) " +
                        ") timestamp(ts) partition by DAY",
                "ts",
                false
        );
    }

    @Test
    public void testSampleByAlignToCalendarFillNullWithKey2() throws Exception {
        assertQuery(
                """
                        ts\ts\tfirst\tavg\tlast\tmax
                        2022-12-01T00:00:00.000000000Z\ts1\t1\t1.5\t2\t2
                        2022-12-01T00:01:00.000000000Z\ts1\tnull\tnull\tnull\tnull
                        2022-12-01T00:02:00.000000000Z\ts1\tnull\tnull\tnull\tnull
                        """,
                "select * from (" +
                        "select ts, s, first(val), avg(val), last(val), max(val)" +
                        "from x " +
                        "sample by 1m fill(null) align to calendar  )" +
                        "where s != 's2' ",
                "create table x as " +
                        "(" +
                        "select '2022-12-01T00:00:30.000000000Z'::timestamp_ns as ts, 's1' as s, 1 as val union all " +
                        "select '2022-12-01T00:00:35.000000000Z'::timestamp_ns, 's1', 2 union all " +
                        "select '2022-12-01T00:01:36.000000000Z'::timestamp_ns, 's2', 2 union all " +
                        "select '2022-12-01T00:02:31.000000000Z'::timestamp_ns, 's2', 3 from long_sequence(1) " +
                        ") timestamp(ts) partition by DAY",
                "ts",
                false
        );
    }

    @Test
    public void testSampleByAlignToCalendarWithoutTimezoneNorOffsetAndLimit() throws Exception {
        assertQuery(
                """
                        k\tcount
                        1970-01-03T00:00:00.000000000Z\t6
                        """,
                "select k, count() from x sample by 6h ALIGN TO CALENDAR limit 1;",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_geohash(30) b," +
                        " timestamp_sequence_ns(172800000001001, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE", "k",
                true,
                true
        );
    }

    @Test
    public void testSampleByAlignToFirstObservationFillNoneWithKey() throws Exception {
        assertQuery(
                """
                        ts\ts\tfirst\tavg\tlast\tmax
                        2022-12-01T00:01:30.000000000Z\ts2\t3\t3.0\t3\t3
                        """,
                "select * from (" +
                        "select ts, s, first(val), avg(val), last(val), max(val)" +
                        "from x " +
                        "sample by 1m align to first observation)" +
                        "where s != 's1' ",
                "create table x as " +
                        "(" +
                        "select '2022-12-01T00:00:30.000000000Z'::timestamp_ns as ts, 's1' as s, 1 as val union all " +
                        "select '2022-12-01T00:00:35.000000000Z'::timestamp_ns, 's1', 2 union all " +
                        "select '2022-12-01T00:01:31.000000000Z'::timestamp_ns, 's2', 3 from long_sequence(1) " +
                        ") timestamp(ts) partition by DAY",
                "ts",
                false
        );
    }

    @Test
    public void testSampleByAlignToFirstObservationFillNoneWithoutKey() throws Exception {
        assertQuery(
                """
                        ts\tfirst\tavg\tlast\tmax
                        2022-12-01T00:01:30.000000000Z\t3\t3.0\t3\t3
                        """,
                "select * from (" +
                        "select ts, first(val), avg(val), last(val), max(val)" +
                        "from x " +
                        "sample by 1m align to first observation)" +
                        "where ts > '2022-12-01T00:00:30.000000000Z' ",
                "create table x as " +
                        "(" +
                        "select '2022-12-01T00:00:30.000000000Z'::timestamp_ns as ts, 1 as val union all " +
                        "select '2022-12-01T00:00:35.000000000Z'::timestamp_ns, 2 union all " +
                        "select '2022-12-01T00:01:31.000000000Z'::timestamp_ns, 3 from long_sequence(1)  " +
                        ") timestamp(ts) partition by DAY",
                "ts",
                false
        );
    }

    @Test
    public void testSampleByAlignedToCalendarWithTimezoneAndLimit() throws Exception {
        assertQuery(
                """
                        k\tcount
                        1970-01-03T00:00:00.000000000Z\t6
                        """,
                "select k, count() from x sample by 6h ALIGN TO CALENDAR TIME ZONE 'UTC' LIMIT 1;",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_geohash(30) b," +
                        " timestamp_sequence_ns(172800000000001, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                true,
                true
        );
    }

    @Test
    public void testSampleByAlignedToCalendarWithTimezoneEndingWithSemicolon() throws Exception {
        assertQuery("""
                        k\tcount
                        1970-01-03T00:00:00.000000000Z\t6
                        1970-01-03T06:00:00.000000000Z\t6
                        1970-01-03T12:00:00.000000000Z\t6
                        1970-01-03T18:00:00.000000000Z\t2
                        """,
                "select k, count() from x sample by 6h ALIGN TO CALENDAR TIME ZONE 'UTC';",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_geohash(30) b," +
                        " timestamp_sequence_ns(172800000000001, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE", "k",
                true,
                true
        );
    }

    @Test
    public void testSampleByAllTypesAndInvalidTimestampColumn() throws Exception {
        assertException(
                """
                        select\s
                            LastUpdate,\s
                            CountryRegion,\s
                            last(Confirmed) Confirmed,\s
                            last(Recovered) Recovered,\s
                            last(Deaths) Deaths\s
                            from (
                                select\s
                                    LastUpdate,\s
                                    CountryRegion,\s
                                    sum(Confirmed) Confirmed,\s
                                    sum(Recovered) Recovered,\s
                                    sum(Deaths) Deaths
                                from (
                                    select\s
                                        LastUpdate,\s
                                        ProvinceState,\s
                                        CountryRegion,\s
                                        last(Confirmed) Confirmed,\s
                                        last(Recovered) Recovered,\s
                                        last(Deaths) Deaths
                                    from (covid where CountryRegion in ('China', 'Mainland China'))
                                    sample by 1d fill(prev)
                                )
                            ) timestamp(xy) sample by 1M
                        ;
                        """,
                "create table covid as " +
                        "(" +
                        "select" +
                        " rnd_symbol(5,4,4,1) ProvinceState," +
                        " rnd_symbol(5,4,4,1) CountryRegion," +
                        " abs(rnd_int()) Confirmed," +
                        " abs(rnd_int()) Recovered," +
                        " abs(rnd_int()) Deaths," +
                        " timestamp_sequence_ns(172800000000001, 3600000000000) LastUpdate" +
                        " from" +
                        " long_sequence(1000)" +
                        ") timestamp(LastUpdate) partition by NONE",
                713,
                "Invalid column: xy"
        );
    }

    @Test
    public void testSampleByAllTypesAndInvalidTimestampType() throws Exception {
        assertException(
                """
                        select\s
                            LastUpdate,\s
                            CountryRegion,\s
                            last(Confirmed) Confirmed,\s
                            last(Recovered) Recovered,\s
                            last(Deaths) Deaths\s
                            from (
                                select\s
                                    LastUpdate,\s
                                    CountryRegion,\s
                                    sum(Confirmed) Confirmed,\s
                                    sum(Recovered) Recovered,\s
                                    sum(Deaths) Deaths
                                from (
                                    select\s
                                        LastUpdate,\s
                                        ProvinceState,\s
                                        CountryRegion,\s
                                        last(Confirmed) Confirmed,\s
                                        last(Recovered) Recovered,\s
                                        last(Deaths) Deaths
                                    from (covid where CountryRegion in ('China', 'Mainland China'))
                                    sample by 1d fill(prev) align to first observation
                                )
                            ) timestamp(CountryRegion) sample by 1M align to first observation
                        ;
                        """,
                "create table covid as " +
                        "(" +
                        "select" +
                        " rnd_symbol(5,4,4,1) ProvinceState," +
                        " rnd_symbol(5,4,4,1) CountryRegion," +
                        " abs(rnd_int()) Confirmed," +
                        " abs(rnd_int()) Recovered," +
                        " abs(rnd_int()) Deaths," +
                        " timestamp_sequence_ns(172800000000001, 3600000000000) LastUpdate" +
                        " from" +
                        " long_sequence(1000)" +
                        ") timestamp(LastUpdate) partition by NONE",
                740, // this is the correct position of the "timestamp(CountryRegion)" column reference
                "not a TIMESTAMP"
        );
    }

    @Test
    public void testSampleByAllowsPredicatePushDown() throws Exception {
        String plan = """
                Radix sort light
                  keys: [tstmp]
                    Filter filter: (tstmp>=2022-12-01T00:00:00.000000000Z and 0<length(sym)*tstmp::long)
                        Async JIT Group By workers: 1
                          keys: [tstmp,sym]
                          values: [first(val),avg(val),last(val),max(val)]
                          filter: sym='B'
                            PageFrame
                                Row forward scan
                                Frame forward scan on: #TABLE#
                """;

        testSampleByPushdown("", "align to calendar", plan);
        testSampleByPushdown("none", "align to calendar", plan);
    }

    @Test
    public void testSampleByAllowsPredicatePushDownWhenTsIsNotIncludedInColumnList() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table if not exists x (  ts1 timestamp_ns, ts2 timestamp_ns, sym symbol, val long ) timestamp(ts1) partition by DAY");
            assertPlanNoLeakCheck(
                    "select * from (" +
                            "select ts2 as tstmp, sym, first(val), avg(val), last(val), max(val) " +
                            "from x " +
                            "sample by 1m align to calendar ) " +
                            "where tstmp >= '2022-12-01T00:00:00.000000000Z' and  sym = 'B' and length(sym)*tstmp::long > 0",
                    """
                            SelectedRecord
                                Radix sort light
                                  keys: [ts1]
                                    Async Group By workers: 1
                                      keys: [tstmp,sym,ts1]
                                      values: [first(val),avg(val),last(val),max(val)]
                                      filter: (ts2>=2022-12-01T00:00:00.000000000Z and sym='B' and 0<length(sym)*ts2::long)
                                        PageFrame
                                            Row forward scan
                                            Frame forward scan on: x
                            """
            );
        });
    }

    @Test
    public void testSampleByBrokenTimestampClause() throws Exception {
        assertMemoryLeak(() -> {
            execute(
                    "CREATE TABLE 'trades' ( " +
                            "  symbol SYMBOL, " +
                            "  side SYMBOL, " +
                            "  price DOUBLE, " +
                            "  amount DOUBLE, " +
                            "  timestamp TIMESTAMP_NS " +
                            ") timestamp(timestamp) PARTITION BY DAY;"
            );

            assertExceptionNoLeakCheck(
                    "SELECT " +
                            "    min(price) AS min_ltp, " +
                            "    max(price) AS max_ltp, " +
                            "    timestamp(timestamp) AS hour " +
                            "FROM trades " +
                            "WHERE timestamp > '2021-03-21' and symbol='ETH-USD' " +
                            "SAMPLE BY 1h " +
                            "ORDER BY hour ASC;",
                    65,
                    "unknown function name: timestamp(TIMESTAMP_NS)"
            );
        });
    }

    @Test
    public void testSampleByCountWithNoTsColSelected() throws Exception {
        assertQuery(
                """
                        count
                        300
                        300
                        300
                        100
                        """,
                "select count() from x sample by 1h align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " timestamp_sequence_ns(172800000000001, 12000000000) k" +
                        " from" +
                        " long_sequence(1000)" +
                        ") timestamp(k) partition by NONE",
                null,
                false
        );
    }

    @Test
    public void testSampleByDayNoFillAlignToCalendarWithTimezoneLondon() throws Exception {
        assertQuery(
                """
                        to_timezone\ts\tlat\tlon
                        2021-03-26T00:00:00.000000000Z\ta\t142.30215575416736\t2021-03-26T22:50:00.000000000Z
                        2021-03-27T00:00:00.000000000Z\ta\tnull\t2021-03-27T23:00:00.000000000Z
                        2021-03-28T00:00:00.000000000Z\ta\t33.45558404694713\t2021-03-28T20:40:00.000000000Z
                        2021-03-29T00:00:00.000000000Z\ta\t70.00560222114518\t2021-03-29T16:40:00.000000000Z
                        2021-03-30T00:00:00.000000000Z\ta\t13.290235514836048\t2021-03-30T02:40:00.000000000Z
                        """,
                "select to_timezone(k, 'Europe/London'), s, lat, lon from (select k, s, first(lat) lat, last(k) lon " +
                        "from x " +
                        "where s in ('a') " +
                        "sample by 1d align to calendar time zone 'Europe/London')",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns('2021-03-25T23:30:00.00000000Z', 50 * 60 * 1000000000L) k" +
                        "   from" +
                        "   long_sequence(120)" +
                        ") timestamp(k)",
                null,
                true,
                true
        );
    }

    @Test
    public void testSampleByDayNoFillNotKeyedAlignToCalendarTimezone() throws Exception {
        assertQuery(
                """
                        k\tc\ta\tlk
                        2021-03-27T00:00:00.000000000Z\t218\t78.61254708288084\t2021-03-27T21:57:00.000000000Z
                        2021-03-28T00:00:00.000000000Z\t230\t16.41641076342043\t2021-03-28T20:57:00.000000000Z
                        2021-03-29T00:00:00.000000000Z\t240\t10.130283315402789\t2021-03-29T20:57:00.000000000Z
                        2021-03-30T00:00:00.000000000Z\t240\t22.52165473191222\t2021-03-30T20:57:00.000000000Z
                        2021-03-31T00:00:00.000000000Z\t72\t45.38592869415369\t2021-03-31T04:09:00.000000000Z
                        """,
                "select to_timezone(k, 'Europe/Riga') k, c, a, lk from (select k, count() c, last(a) a, last(k) lk from x sample by 1d align to calendar time zone 'Europe/Riga')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-03-27T00:15:00.000000000Z' as timestamp_ns), 6*60000000000) k" +
                        " from" +
                        " long_sequence(1000)" +
                        ") timestamp(k) partition by NONE",
                null,
                true,
                true
        );
    }

    @Test
    public void testSampleByDayNoFillNotKeyedAlignToCalendarTimezoneOct() throws Exception {
        // We are going over spring time change. Because time is "expanding" we dont have
        // to do anything special. Our UTC timestamps will show "gap" and data doesn't
        // have to change
        assertQuery(
                """
                        k\tc
                        2021-10-30T00:00:00.000000000Z\t218
                        2021-10-31T00:00:00.000000000Z\t250
                        2021-11-01T00:00:00.000000000Z\t132
                        """,
                "select to_timezone(k, 'Europe/Berlin') k, c from (select k, count() c from x sample by 1d align to calendar time zone 'Europe/Berlin')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-10-30T00:15:00.000000000Z' as timestamp_ns), 6*60000000000) k" +
                        " from" +
                        " long_sequence(600)" +
                        ") timestamp(k) partition by NONE",
                null,
                true,
                true
        );
    }

    @Test
    public void testSampleByDayNoFillNotKeyedAlignToCalendarWithTimezoneLondon() throws Exception {
        assertQuery(
                """
                        to_timezone\tlat\tlon
                        2021-03-26T00:00:00.000000000Z\t142.30215575416736\t2021-03-26T22:50:00.000000000Z
                        2021-03-27T00:00:00.000000000Z\tnull\t2021-03-27T23:00:00.000000000Z
                        2021-03-28T00:00:00.000000000Z\t33.45558404694713\t2021-03-28T20:40:00.000000000Z
                        2021-03-29T00:00:00.000000000Z\t70.00560222114518\t2021-03-29T16:40:00.000000000Z
                        2021-03-30T00:00:00.000000000Z\t13.290235514836048\t2021-03-30T02:40:00.000000000Z
                        """,
                "select to_timezone(k, 'Europe/London'), lat, lon from (select k, first(lat) lat, last(k) lon " +
                        "from x " +
                        "where s in ('a') " +
                        "sample by 1d align to calendar time zone 'Europe/London')",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns('2021-03-25T23:30:00.00000000Z', 50 * 60 * 1000000000L) k" +
                        "   from" +
                        "   long_sequence(120)" +
                        ") timestamp(k)",
                null,
                true,
                true
        );
    }

    @Test
    public void testSampleByDisallowsPredicatePushdown() throws Exception {
        String align = "";

        for (String fill : Arrays.asList("", "none", "null", "linear", "prev")) {
            if (isNone(fill)) {
                continue;
            }

            String plan = "Filter filter: (tstmp>=2022-12-01T00:00:00.000000000Z and sym='B' and 0<length(sym)*tstmp::long)\n" +
                    "    Sample By\n" +
                    (isNone(fill) ? "" : "      fill: " + fill + "\n") +
                    "      keys: [tstmp,sym]\n" +
                    "      values: [first(val),avg(val),last(val),max(val)]\n" +
                    "        SelectedRecord\n" +
                    "            PageFrame\n" +
                    "                Row forward scan\n" +
                    "                Frame forward scan on: #TABLE#\n";

            testSampleByPushdown(fill, align, plan);
        }

        align = "align to first observation";

        for (String fill : Arrays.asList("", "none", "null", "linear", "prev")) {

            String plan = "Filter filter: (tstmp>=2022-12-01T00:00:00.000000000Z and sym='B' and 0<length(sym)*tstmp::long)\n" +
                    "    Sample By\n" +
                    (isNone(fill) ? "" : "      fill: " + fill + "\n") +
                    "      keys: [tstmp,sym]\n" +
                    "      values: [first(val),avg(val),last(val),max(val)]\n" +
                    "        SelectedRecord\n" +
                    "            PageFrame\n" +
                    "                Row forward scan\n" +
                    "                Frame forward scan on: #TABLE#\n";

            testSampleByPushdown(fill, align, plan);
        }

        align = "align to calendar";

        for (String fill : Arrays.asList("", "none", "null", "linear", "prev")) {
            if (isNone(fill)) {
                continue;
            }

            String plan = "Filter filter: (tstmp>=2022-12-01T00:00:00.000000000Z and sym='B' and 0<length(sym)*tstmp::long)\n" +
                    "    Sample By\n" +
                    (isNone(fill) ? "" : "      fill: " + fill + "\n") +
                    "      keys: [tstmp,sym]\n" +
                    "      values: [first(val),avg(val),last(val),max(val)]\n" +
                    "        SelectedRecord\n" +
                    "            PageFrame\n" +
                    "                Row forward scan\n" +
                    "                Frame forward scan on: #TABLE#\n";

            testSampleByPushdown(fill, align, plan);
        }

    }

    @Test
    public void testSampleByDoesntAllowNonTimestampPredicatePushdown() throws Exception {
        assertMemoryLeak(() -> {
            execute("""
                    create table tab as (
                    select dateadd('m', 11*x::int, '2022-12-01T01:00:00.000000000Z') ts, x v, rnd_str('A', 'B') s
                    from long_sequence(6) ) timestamp(ts)""");

            assertQueryNoLeakCheck("""
                            ts\tv\ts
                            2022-12-01T01:11:00.000000000Z\t1\tA
                            2022-12-01T01:22:00.000000000Z\t2\tA
                            2022-12-01T01:33:00.000000000Z\t3\tB
                            2022-12-01T01:44:00.000000000Z\t4\tB
                            2022-12-01T01:55:00.000000000Z\t5\tB
                            2022-12-01T02:06:00.000000000Z\t6\tB
                            """,
                    "select * from tab",
                    "ts",
                    true,
                    true
            );

            assertPlanNoLeakCheck(
                    "select * from (select ts, s, first(v) from tab sample by 30m fill(prev) align to first observation) where s = 'B'",
                    """
                            SelectedRecord
                                Filter filter: s='B'
                                    Sample By
                                      fill: prev
                                      keys: [s,ts]
                                      values: [first(v)]
                                        PageFrame
                                            Row forward scan
                                            Frame forward scan on: tab
                            """
            );

            assertQueryNoLeakCheck(
                    """
                            ts\ts\tfirst
                            2022-12-01T01:11:00.000000000Z\tB\t3
                            2022-12-01T01:41:00.000000000Z\tB\t4
                            """,
                    "select * from (select ts, s, first(v) from tab sample by 30m fill(prev) align to first observation) where s = 'B' ",
                    "ts",
                    false
            );
        });
    }

    @Test
    public void testSampleByDoesntAllowTimestampPredicatePushdown() throws Exception {
        assertMemoryLeak(() -> {
            execute("""
                    create table tab as (
                    select dateadd('m', 10*x::int, '2022-12-01T01:00:00.000000000Z') ts, x v
                    from long_sequence(6) ) timestamp(ts)""");

            assertPlanNoLeakCheck(
                    "select * from (select ts, first(v) from tab sample by 30m fill(prev) align to first observation) where ts > '2022-12-01T01:10:00.000000000Z'",
                    """
                            Filter filter: 2022-12-01T01:10:00.000000000Z<ts
                                Sample By
                                  fill: prev
                                  values: [first(v)]
                                    PageFrame
                                        Row forward scan
                                        Frame forward scan on: tab
                            """
            );

            assertQueryNoLeakCheck(
                    """
                            ts\tfirst
                            2022-12-01T01:40:00.000000000Z\t4
                            """,
                    "select * from (select ts, first(v) from tab sample by 30m fill(prev) align to first observation) where ts > '2022-12-01T01:10:00.000000000Z' ",
                    "ts",
                    false
            );
        });
    }

    @Test
    public void testSampleByDstForwardShift() throws Exception {
        // Although '00:15' offset here pushes certain bucket timestamps to the gap hour
        // in 'Europe/Prague' time zone (2021-03-28T02:00 - 2021-03-28T03:00), timestamp_floor()
        // function used in sample by rewrite should assign them to the previous bucket, so
        // that there are no duplicate timestamps returned after backward conversion to UTC.
        assertQuery(
                """
                        k\ts\tlat\tlon
                        2021-03-28T00:15:00.000000000Z\ta\t144.77803379943109\tnull
                        2021-03-28T01:15:00.000000000Z\ta\t31.267026583720984\tnull
                        2021-03-28T02:15:00.000000000Z\ta\t103.7167928478985\t128.42101395467057
                        """,
                "select k, s, first(lat) lat, last(lon) lon " +
                        "from x " +
                        "where s in ('a') " +
                        "sample by 1h align to calendar time zone 'Europe/Prague' with offset '00:15'",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a') s," +
                        "   timestamp_sequence_ns('2021-03-28T00:59:00.00000000Z', 60*1000000000L) k" +
                        "   from" +
                        "   long_sequence(100)" +
                        "), index(s) timestamp(k) partition by DAY",
                "k",
                true,
                true
        );
    }

    @Test
    public void testSampleByFilteredByIndex() throws Exception {
        assertQuery(
                """
                        time\ts1\tdd
                        2023-05-16T00:04:00.000000000Z\ta\tnull
                        2023-05-16T00:05:00.000000000Z\ta\t0.5243722859289777
                        2023-05-16T00:08:00.000000000Z\tc\t0.1985581797355932
                        2023-05-16T00:07:00.000000000Z\tb\t0.6778564558839208
                        2023-05-16T00:10:00.000000000Z\tb\t0.21583224269349388
                        """,
                "SELECT last(ts) as time, s1, last(d1) as dd " +
                        "FROM x " +
                        "WHERE ts BETWEEN '2023-05-16T00:00:00.00Z' AND '2023-05-16T00:10:00.00Z' " +
                        "AND s2 = ('foo') " +
                        "SAMPLE BY 5m ALIGN TO FIRST OBSERVATION;",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_symbol('a','b','c') s1," +
                        "   rnd_symbol('foo','bar') s2," +
                        "   rnd_double(1) d1," +
                        "   timestamp_sequence_ns('2023-05-16T00:00:00.00000000Z', 60*1000000000L) ts" +
                        "   from long_sequence(100)" +
                        "), index(s1), index(s2) timestamp(ts) partition by DAY",
                null,
                false
        );

        assertQuery(
                """
                        time\ts1\tdd
                        2023-05-16T00:04:00.000000000Z\ta\tnull
                        2023-05-16T00:05:00.000000000Z\ta\t0.5243722859289777
                        2023-05-16T00:08:00.000000000Z\tc\t0.1985581797355932
                        2023-05-16T00:07:00.000000000Z\tb\t0.6778564558839208
                        2023-05-16T00:10:00.000000000Z\tb\t0.21583224269349388
                        """,
                "SELECT last(ts) as time, s1, last(d1) as dd " +
                        "FROM x " +
                        "WHERE ts BETWEEN '2023-05-16T00:00:00.00Z' AND '2023-05-16T00:10:00.00Z' " +
                        "AND s2 = ('foo') " +
                        "SAMPLE BY 5m ALIGN TO CALENDAR;",
                null,
                true,
                true
        );
    }

    @Test
    public void testSampleByFirstLastFactoryIsChosenIfNotKeyedByFilteredSymbol() throws Exception {
        assertMemoryLeak(() -> {
            execute("CREATE TABLE pos (" +
                    "  time TIMESTAMP_NS," +
                    "  ts TIMESTAMP_NS," +
                    "  id SYMBOL INDEX," +
                    "  lat DOUBLE," +
                    "  lon DOUBLE," +
                    "  geo6 GEOHASH(6c)" +
                    ") timestamp (time) PARTITION BY DAY;");

            assertPlanNoLeakCheck(
                    "select time, last(lat) lat, last(lon) lon " +
                            " from pos " +
                            " where id = 'A' sample by 15m ALIGN to CALENDAR",
                    """
                            Radix sort light
                              keys: [time]
                                GroupBy vectorized: false
                                  keys: [time]
                                  values: [last(lat),last(lon)]
                                    DeferredSingleSymbolFilterPageFrame
                                        Index forward scan on: id deferred: true
                                          filter: id='A'
                                        Frame forward scan on: pos
                            """
            );
        });
    }

    @Test
    public void testSampleByFirstLastFactoryIsNotChosenIfKeyedByNonDesignatedTimestamp() throws Exception {
        assertMemoryLeak(() -> {
            execute("CREATE TABLE pos (" +
                    "  time TIMESTAMP_NS," +
                    "  ts TIMESTAMP_NS," +
                    "  id SYMBOL INDEX," +
                    "  lat DOUBLE," +
                    "  lon DOUBLE," +
                    "  geo6 GEOHASH(6c)" +
                    ") timestamp (time) PARTITION BY DAY;");

            assertPlanNoLeakCheck(
                    "select   id, time, ts, last(lat) lat, last(lon) lon " +
                            " from pos " +
                            " where id = 'A' sample by 15m ALIGN to CALENDAR",
                    """
                            Radix sort light
                              keys: [time]
                                GroupBy vectorized: false
                                  keys: [id,time,ts]
                                  values: [last(lat),last(lon)]
                                    DeferredSingleSymbolFilterPageFrame
                                        Index forward scan on: id deferred: true
                                          filter: id='A'
                                        Frame forward scan on: pos
                            """
            );
        });
    }

    @Test
    public void testSampleByFirstLastFactoryIsNotChosenIfKeyedByNonFilteredSymbol() throws Exception {
        assertMemoryLeak(() -> {
            execute("CREATE TABLE pos (" +
                    "  time TIMESTAMP_NS," +
                    "  id SYMBOL INDEX," +
                    "  lat DOUBLE," +
                    "  lon DOUBLE," +
                    "  geo6 GEOHASH(6c)," +
                    "  type SYMBOL " +
                    ") timestamp (time) PARTITION BY DAY;");

            assertPlanNoLeakCheck(
                    "select time, type, last(lat) lat, last(lon) lon " +
                            " from pos " +
                            " where id = 'A' sample by 15m ALIGN to CALENDAR",
                    """
                            Radix sort light
                              keys: [time]
                                GroupBy vectorized: false
                                  keys: [time,type]
                                  values: [last(lat),last(lon)]
                                    DeferredSingleSymbolFilterPageFrame
                                        Index forward scan on: id deferred: true
                                          filter: id='A'
                                        Frame forward scan on: pos
                            """
            );

            assertPlanNoLeakCheck(
                    "select   id, time, type, last(lat) lat, last(lon) lon " +
                            " from pos " +
                            " where id = 'A' sample by 15m ALIGN to CALENDAR",
                    """
                            Radix sort light
                              keys: [time]
                                GroupBy vectorized: false
                                  keys: [id,time,type]
                                  values: [last(lat),last(lon)]
                                    DeferredSingleSymbolFilterPageFrame
                                        Index forward scan on: id deferred: true
                                          filter: id='A'
                                        Frame forward scan on: pos
                            """
            );
        });
    }

    @Test
    public void testSampleByFirstLastFactoryIsNotChosenIfKeyedByNonSymbol() throws Exception {
        assertMemoryLeak(() -> {
            execute("CREATE TABLE pos (" +
                    "  time TIMESTAMP_NS," +
                    "  id SYMBOL INDEX," +
                    "  lat DOUBLE," +
                    "  lon DOUBLE," +
                    "  geo6 GEOHASH(6c)" +
                    ") timestamp (time) PARTITION BY DAY;");

            assertPlanNoLeakCheck(
                    "select   id, time, geo6, last(lat) lat, last(lon) lon " +
                            " from pos " +
                            " where id = 'A' sample by 15m ALIGN to CALENDAR",
                    """
                            Radix sort light
                              keys: [time]
                                GroupBy vectorized: false
                                  keys: [id,time,geo6]
                                  values: [last(lat),last(lon)]
                                    DeferredSingleSymbolFilterPageFrame
                                        Index forward scan on: id deferred: true
                                          filter: id='A'
                                        Frame forward scan on: pos
                            """
            );

            assertPlanNoLeakCheck(
                    "select   id, time, lat, last(lat) lastlat, last(lon) lon " +
                            " from pos " +
                            " where id = 'A' sample by 15m ALIGN to CALENDAR",
                    """
                            Radix sort light
                              keys: [time]
                                GroupBy vectorized: false
                                  keys: [id,time,lat]
                                  values: [last(lat),last(lon)]
                                    DeferredSingleSymbolFilterPageFrame
                                        Index forward scan on: id deferred: true
                                          filter: id='A'
                                        Frame forward scan on: pos
                            """
            );
        });
    }

    @Test
    public void testSampleByFirstLastIndexFilterByNullConcurrent() throws Exception {
        testSampleByFirstLastIndexedConcurrent(
                """
                        SELECT first(kms) as k_first, last(kms) AS k_last, first(d1) as d1_first, last(d1) as d1_last, first(d2) as d2_first, last(d2) as d2_last
                        FROM x
                        WHERE k BETWEEN '1970-01-01T00:15:33.063Z' AND '1970-01-01T01:15:33.063Z'
                          AND s = null
                        SAMPLE BY 10s;"""
        );
    }

    @Test
    public void testSampleByFirstLastIndexFilterConcurrent() throws Exception {
        testSampleByFirstLastIndexedConcurrent(
                """
                        SELECT first(kms) as k_first, last(kms) AS k_last, first(d1) as d1_first, last(d1) as d1_last, first(d2) as d2_first, last(d2) as d2_last
                        FROM x
                        WHERE k BETWEEN '1970-01-01T00:15:33.063Z' AND '1970-01-01T01:15:33.063Z'
                          AND s = 'a'
                        SAMPLE BY 10s;"""
        );
    }

    @Test
    public void testSampleByFirstLastWithNonTsOrFilteredSymbolColumn() throws Exception {
        assertQuery(
                "id\ttime\tgeo6\tlat\tlon\n",
                "select   id, time, geo6, last(lat) lat, last(lon) lon " +
                        "from pos " +
                        "where id = 'A' sample by 15m ALIGN to CALENDAR " +
                        "order by time, id",
                "CREATE TABLE pos (" +
                        "  time TIMESTAMP_NS," +
                        "  id SYMBOL INDEX," +
                        "  lat DOUBLE," +
                        "  lon DOUBLE," +
                        "  geo6 GEOHASH(6c)" +
                        ") timestamp (time) PARTITION BY DAY",
                "time",
                "insert into pos " +
                        "select dateadd('m',x::int, '1970-01-01T00:00:00.000000000Z') , 'A', x, x, " +
                        "case when x%2 = 0 then 'yyyyyy' else 'zzzzzz' end  from long_sequence(40) " +
                        "union all " +
                        "select '1970-01-01T01:01:00.000000000Z'::timestamp, 'A', 101, 101, #zzzzzz from long_sequence(1)",
                """
                        id\ttime\tgeo6\tlat\tlon
                        A\t1970-01-01T00:00:00.000000000Z\tzzzzzz\t13.0\t13.0
                        A\t1970-01-01T00:00:00.000000000Z\tyyyyyy\t14.0\t14.0
                        A\t1970-01-01T00:15:00.000000000Z\tzzzzzz\t29.0\t29.0
                        A\t1970-01-01T00:15:00.000000000Z\tyyyyyy\t28.0\t28.0
                        A\t1970-01-01T00:30:00.000000000Z\tyyyyyy\t40.0\t40.0
                        A\t1970-01-01T00:30:00.000000000Z\tzzzzzz\t39.0\t39.0
                        A\t1970-01-01T01:00:00.000000000Z\tzzzzzz\t101.0\t101.0
                        """,
                true,
                true,
                false
        );
    }

    @Test
    public void testSampleByFromToBindVariables() throws Exception {
        assertMemoryLeak(() -> {
            execute(FROM_TO_DDL, sqlExecutionContext);

            snapshotMemoryUsage();
            try (
                    final RecordCursorFactory factory = select(
                            "select ts, avg(x) from fromto\n" +
                                    "sample by 5d from $1 to $2 fill(42)")
            ) {
                final String expected = """
                        ts\tavg
                        2017-12-20T00:00:00.000000000Z\t42.0
                        2017-12-25T00:00:00.000000000Z\t42.0
                        2017-12-30T00:00:00.000000000Z\t72.5
                        2018-01-04T00:00:00.000000000Z\t264.5
                        2018-01-09T00:00:00.000000000Z\t432.5
                        2018-01-14T00:00:00.000000000Z\t42.0
                        2018-01-19T00:00:00.000000000Z\t42.0
                        2018-01-24T00:00:00.000000000Z\t42.0
                        2018-01-29T00:00:00.000000000Z\t42.0
                        """;

                sqlExecutionContext.getBindVariableService().setStr(0, "2017-12-20");
                sqlExecutionContext.getBindVariableService().setStr(1, "2018-01-31");

                try (final RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                    assertCursor(expected, cursor, factory.getMetadata(), true);
                }

                assertFactoryMemoryUsage();
            }
        });
    }

    @Test
    public void testSampleByFromToFillNull() throws Exception {
        assertMemoryLeak(() -> {
            execute(FROM_TO_DDL);
            drainWalQueue();
            assertSql(
                    """
                            ts\tavg
                            2017-12-30T00:00:00.000000000Z\t72.5
                            2018-01-04T00:00:00.000000000Z\t264.5
                            2018-01-09T00:00:00.000000000Z\t432.5
                            """,
                    "select ts, avg(x) from fromto\n" +
                            "sample by 5d"
            );
            assertSql(
                    """
                            ts\tavg
                            2017-12-20T00:00:00.000000000Z\tnull
                            2017-12-25T00:00:00.000000000Z\tnull
                            2017-12-30T00:00:00.000000000Z\t72.5
                            2018-01-04T00:00:00.000000000Z\t264.5
                            2018-01-09T00:00:00.000000000Z\t432.5
                            """,
                    "select ts, avg(x) from fromto\n" +
                            "sample by 5d from '2017-12-20' fill(null)"
            );
            assertSql(
                    """
                            ts\tavg
                            2017-12-20T00:00:00.000000000Z\tnull
                            2017-12-25T00:00:00.000000000Z\tnull
                            2017-12-30T00:00:00.000000000Z\t72.5
                            2018-01-04T00:00:00.000000000Z\t264.5
                            2018-01-09T00:00:00.000000000Z\t432.5
                            2018-01-14T00:00:00.000000000Z\tnull
                            2018-01-19T00:00:00.000000000Z\tnull
                            2018-01-24T00:00:00.000000000Z\tnull
                            2018-01-29T00:00:00.000000000Z\tnull
                            """,
                    "select ts, avg(x) from fromto\n" +
                            "sample by 5d from '2017-12-20' to '2018-01-31' fill(null)"
            );
        });
    }

    @Test
    public void testSampleByFromToIsDisallowedForKeyedQueries() throws Exception {
        assertMemoryLeak(() -> {
            execute(FROM_TO_DDL);
            assertException("""
                            select ts, avg(x), first(x), last(x), x from fromto
                            where s != '5'
                            sample by 5d from '2017-12-20' to '2018-01-31' fill(42)""",
                    0, "supported");
            assertException("""
                    select ts, avg(x), first(x), last(x), x from fromto
                    where s != '5'
                    sample by 5d from '2017-12-20' to '2018-01-31' fill(42)""", 0, "supported");
        });
    }

    @Test
    public void testSampleByFromToNoFill() throws Exception {
        assertMemoryLeak(() -> {
            execute(FROM_TO_DDL);
            drainWalQueue();
            assertSql(
                    """
                            ts\tavg
                            2017-12-30T00:00:00.000000000Z\t72.5
                            2018-01-04T00:00:00.000000000Z\t264.5
                            2018-01-09T00:00:00.000000000Z\t432.5
                            """,
                    "select ts, avg(x) from fromto\n" +
                            "sample by 5d"
            );
            assertSql(
                    """
                            ts\tavg
                            2017-12-30T00:00:00.000000000Z\t72.5
                            2018-01-04T00:00:00.000000000Z\t264.5
                            2018-01-09T00:00:00.000000000Z\t432.5
                            """,
                    "select ts, avg(x) from fromto\n" +
                            "sample by 5d from '2017-12-20'"
            );
            assertSql(
                    """
                            ts\tavg
                            2018-01-01T00:00:00.000000000Z\t120.5
                            2018-01-06T00:00:00.000000000Z\t360.5
                            """,
                    "select ts, avg(x) from fromto\n" +
                            "sample by 5d from '2018-01-01'"
            );
            assertSql(
                    """
                            ts\tavg
                            2018-01-01T00:00:00.000000000Z\t120.5
                            2018-01-06T00:00:00.000000000Z\t360.5
                            """,
                    "select ts, avg(x) from fromto\n" +
                            "sample by 5d from '2018-01-01' to '2018-01-31'"
            );
        });
    }

    @Test
    public void testSampleByFromToPlans() throws Exception {
        assertMemoryLeak(() -> {
            execute(
                    """
                            create table tbl (
                              ts timestamp_ns,
                              price double
                            ) timestamp(ts) partition by day wal;"""
            );
            drainWalQueue();

            assertPlanNoLeakCheck(
                    "select ts, avg(price) from tbl sample by 5m from '2018-01-01' to '2019-01-01' align to calendar with offset '10:00'",
                    """
                            Radix sort light
                              keys: [ts]
                                Async Group By workers: 1
                                  keys: [ts]
                                  values: [avg(price)]
                                  filter: null
                                    PageFrame
                                        Row forward scan
                                        Interval forward scan on: tbl
                                          intervals: [("2018-01-01T00:00:00.000000000Z","2018-12-31T23:59:59.999999999Z")]
                            """
            );

            assertPlanNoLeakCheck(
                    "select ts, avg(price) from tbl sample by 5m from '2018-01-01' align to calendar with offset '10:00'",
                    """
                            Radix sort light
                              keys: [ts]
                                Async Group By workers: 1
                                  keys: [ts]
                                  values: [avg(price)]
                                  filter: null
                                    PageFrame
                                        Row forward scan
                                        Interval forward scan on: tbl
                                          intervals: [("2018-01-01T00:00:00.000000000Z","MAX")]
                            """
            );

            assertPlanNoLeakCheck(
                    "select ts, avg(price) from tbl sample by 5m to '2019-01-01' align to calendar with offset '10:00'",
                    """
                            Radix sort light
                              keys: [ts]
                                Async Group By workers: 1
                                  keys: [ts]
                                  values: [avg(price)]
                                  filter: null
                                    PageFrame
                                        Row forward scan
                                        Interval forward scan on: tbl
                                          intervals: [("MIN","2018-12-31T23:59:59.999999999Z")]
                            """
            );

            assertPlanNoLeakCheck(
                    "select ts, avg(price) from tbl sample by 5m align to calendar with offset '10:00'",
                    """
                            Radix sort light
                              keys: [ts]
                                Async Group By workers: 1
                                  keys: [ts]
                                  values: [avg(price)]
                                  filter: null
                                    PageFrame
                                        Row forward scan
                                        Frame forward scan on: tbl
                            """
            );
        });
    }

    @Test
    public void testSampleByFromToSampleByMonthWithFill() throws Exception {
        assertMemoryLeak(() -> {
            execute(FROM_TO_DDL);
            assertSql(
                    """
                            ts\tavg
                            2017-01-01T00:00:00.000000000Z\tnull
                            2017-05-01T00:00:00.000000000Z\tnull
                            2017-09-01T00:00:00.000000000Z\tnull
                            2018-01-01T00:00:00.000000000Z\t240.5
                            2018-05-01T00:00:00.000000000Z\tnull
                            2018-09-01T00:00:00.000000000Z\tnull
                            """,
                    "select ts, avg(x) from fromto\n" +
                            "sample by 4M from '2017-01-01' to '2019-01-01' fill(null)"
            );
        });
    }

    @Test
    public void testSampleByFromToSampleByYearWithFill() throws Exception {
        assertMemoryLeak(() -> {
            execute(FROM_TO_DDL);
            assertSql(
                    """
                            ts\tavg
                            2000-01-01T00:00:00.000000000Z\tnull
                            2004-01-01T00:00:00.000000000Z\tnull
                            2008-01-01T00:00:00.000000000Z\tnull
                            2012-01-01T00:00:00.000000000Z\tnull
                            2016-01-01T00:00:00.000000000Z\t240.5
                            2020-01-01T00:00:00.000000000Z\tnull
                            2024-01-01T00:00:00.000000000Z\tnull
                            2028-01-01T00:00:00.000000000Z\tnull
                            2032-01-01T00:00:00.000000000Z\tnull
                            2036-01-01T00:00:00.000000000Z\tnull
                            2040-01-01T00:00:00.000000000Z\tnull
                            2044-01-01T00:00:00.000000000Z\tnull
                            2048-01-01T00:00:00.000000000Z\tnull
                            """,
                    "select ts, avg(x) from fromto\n" +
                            "sample by 4y from '2000-01-01' to '2050-01-01' fill(null)"
            );
        });
    }

    @Test
    public void testSampleByLastIndexFilterByNullConcurrent() throws Exception {
        testSampleByFirstLastIndexedConcurrent(
                """
                        SELECT last(kms) as k, last(d1) as d1, last(d2) as d2
                        FROM x
                        WHERE k BETWEEN '1970-01-01T00:15:33.063Z' AND '1970-01-01T01:15:33.063Z'
                          AND s = null
                        SAMPLE BY 10s;"""
        );
    }

    @Test
    public void testSampleByLastOnlyIndexFilterConcurrent() throws Exception {
        testSampleByFirstLastIndexedConcurrent(
                """
                        SELECT last(kms) as k, last(d1) as d1, last(d2) as d2
                        FROM x
                        WHERE k BETWEEN '1970-01-01T00:15:33.063Z' AND '1970-01-01T01:15:33.063Z'
                          AND s = 'a'
                        SAMPLE BY 10s;"""
        );
    }

    @Test
    public void testSampleByMicrosFillNoneNotKeyedEmpty() throws Exception {
        String expected = "sum\tk\n";
        String ddl = "create table x" +
                "(" +
                " a double," +
                " b symbol," +
                " k timestamp_ns" +
                ") timestamp(k) partition by NONE";
        String ddl2 = "insert into x select * from (" +
                "select" +
                " rnd_double(0)*100 a," +
                " rnd_symbol(5,4,4,1) b," +
                " timestamp_sequence(277200000000, 100) k" +
                " from" +
                " long_sequence(30)" +
                ") timestamp(k)";

        assertQuery(
                expected,
                "select sum(a), k from x sample by 100U fill(none) align to first observation",
                ddl,
                "k",
                ddl2,
                """
                        sum\tk
                        11.427984775756228\t1970-01-04T05:00:00.000000000Z
                        42.17768841969397\t1970-01-04T05:00:00.000100000Z
                        23.90529010846525\t1970-01-04T05:00:00.000200000Z
                        70.94360487171201\t1970-01-04T05:00:00.000300000Z
                        87.99634725391621\t1970-01-04T05:00:00.000400000Z
                        32.881769076795045\t1970-01-04T05:00:00.000500000Z
                        97.71103146051203\t1970-01-04T05:00:00.000600000Z
                        81.46807944500559\t1970-01-04T05:00:00.000700000Z
                        57.93466326862211\t1970-01-04T05:00:00.000800000Z
                        12.026122412833129\t1970-01-04T05:00:00.000900000Z
                        48.820511018586934\t1970-01-04T05:00:00.001000000Z
                        26.922103479744898\t1970-01-04T05:00:00.001100000Z
                        52.98405941762054\t1970-01-04T05:00:00.001200000Z
                        84.45258177211063\t1970-01-04T05:00:00.001300000Z
                        97.5019885372507\t1970-01-04T05:00:00.001400000Z
                        49.00510449885239\t1970-01-04T05:00:00.001500000Z
                        80.01121139739173\t1970-01-04T05:00:00.001600000Z
                        92.050039469858\t1970-01-04T05:00:00.001700000Z
                        45.6344569609078\t1970-01-04T05:00:00.001800000Z
                        40.455469747939254\t1970-01-04T05:00:00.001900000Z
                        56.594291398612405\t1970-01-04T05:00:00.002000000Z
                        9.750574414434398\t1970-01-04T05:00:00.002100000Z
                        12.105630273556178\t1970-01-04T05:00:00.002200000Z
                        57.78947915182423\t1970-01-04T05:00:00.002300000Z
                        86.85154305419587\t1970-01-04T05:00:00.002400000Z
                        12.02416087573498\t1970-01-04T05:00:00.002500000Z
                        49.42890511958454\t1970-01-04T05:00:00.002600000Z
                        58.912164838797885\t1970-01-04T05:00:00.002700000Z
                        67.52509547112409\t1970-01-04T05:00:00.002800000Z
                        44.80468966861358\t1970-01-04T05:00:00.002900000Z
                        """,
                false
        );

        assertQuery(
                """
                        sum\tk
                        11.427984775756228\t1970-01-04T05:00:00.000000000Z
                        42.17768841969397\t1970-01-04T05:00:00.000100000Z
                        23.90529010846525\t1970-01-04T05:00:00.000200000Z
                        70.94360487171201\t1970-01-04T05:00:00.000300000Z
                        87.99634725391621\t1970-01-04T05:00:00.000400000Z
                        32.881769076795045\t1970-01-04T05:00:00.000500000Z
                        97.71103146051203\t1970-01-04T05:00:00.000600000Z
                        81.46807944500559\t1970-01-04T05:00:00.000700000Z
                        57.93466326862211\t1970-01-04T05:00:00.000800000Z
                        12.026122412833129\t1970-01-04T05:00:00.000900000Z
                        48.820511018586934\t1970-01-04T05:00:00.001000000Z
                        26.922103479744898\t1970-01-04T05:00:00.001100000Z
                        52.98405941762054\t1970-01-04T05:00:00.001200000Z
                        84.45258177211063\t1970-01-04T05:00:00.001300000Z
                        97.5019885372507\t1970-01-04T05:00:00.001400000Z
                        49.00510449885239\t1970-01-04T05:00:00.001500000Z
                        80.01121139739173\t1970-01-04T05:00:00.001600000Z
                        92.050039469858\t1970-01-04T05:00:00.001700000Z
                        45.6344569609078\t1970-01-04T05:00:00.001800000Z
                        40.455469747939254\t1970-01-04T05:00:00.001900000Z
                        56.594291398612405\t1970-01-04T05:00:00.002000000Z
                        9.750574414434398\t1970-01-04T05:00:00.002100000Z
                        12.105630273556178\t1970-01-04T05:00:00.002200000Z
                        57.78947915182423\t1970-01-04T05:00:00.002300000Z
                        86.85154305419587\t1970-01-04T05:00:00.002400000Z
                        12.02416087573498\t1970-01-04T05:00:00.002500000Z
                        49.42890511958454\t1970-01-04T05:00:00.002600000Z
                        58.912164838797885\t1970-01-04T05:00:00.002700000Z
                        67.52509547112409\t1970-01-04T05:00:00.002800000Z
                        44.80468966861358\t1970-01-04T05:00:00.002900000Z
                        """,
                "select sum(a), k from x sample by 100U fill(none) align to calendar",
                "k",
                true,
                true
        );
    }

    @Test
    public void testSampleByMillisFillNoneNotKeyedEmpty() throws Exception {
        String expected = "sum\tk\n";

        String ddl = "create table x as " +
                "(" +
                "select" +
                " rnd_double(0)*100 a," +
                " rnd_symbol(5,4,4,1) b," +
                " timestamp_sequence_ns(172800000000000, 100000) k" +
                " from" +
                " long_sequence(0)" +
                ") timestamp(k) partition by NONE";
        String ddl2 = "insert into x select * from (" +
                "select" +
                " rnd_double(0)*100 a," +
                " rnd_symbol(5,4,4,1) b," +
                " timestamp_sequence(277200000000, 100000) k" +
                " from" +
                " long_sequence(30)" +
                ") timestamp(k)";

        assertQuery(
                expected,
                "select sum(a), k from x sample by 100T fill(none) align to first observation",
                ddl,
                "k",
                ddl2,
                """
                        sum\tk
                        0.35983672154330515\t1970-01-04T05:00:00.000000000Z
                        76.75673070796104\t1970-01-04T05:00:00.100000000Z
                        62.173267078530984\t1970-01-04T05:00:00.200000000Z
                        63.81607531178513\t1970-01-04T05:00:00.300000000Z
                        57.93466326862211\t1970-01-04T05:00:00.400000000Z
                        12.026122412833129\t1970-01-04T05:00:00.500000000Z
                        48.820511018586934\t1970-01-04T05:00:00.600000000Z
                        26.922103479744898\t1970-01-04T05:00:00.700000000Z
                        52.98405941762054\t1970-01-04T05:00:00.800000000Z
                        84.45258177211063\t1970-01-04T05:00:00.900000000Z
                        97.5019885372507\t1970-01-04T05:00:01.000000000Z
                        49.00510449885239\t1970-01-04T05:00:01.100000000Z
                        80.01121139739173\t1970-01-04T05:00:01.200000000Z
                        92.050039469858\t1970-01-04T05:00:01.300000000Z
                        45.6344569609078\t1970-01-04T05:00:01.400000000Z
                        40.455469747939254\t1970-01-04T05:00:01.500000000Z
                        56.594291398612405\t1970-01-04T05:00:01.600000000Z
                        9.750574414434398\t1970-01-04T05:00:01.700000000Z
                        12.105630273556178\t1970-01-04T05:00:01.800000000Z
                        57.78947915182423\t1970-01-04T05:00:01.900000000Z
                        86.85154305419587\t1970-01-04T05:00:02.000000000Z
                        12.02416087573498\t1970-01-04T05:00:02.100000000Z
                        49.42890511958454\t1970-01-04T05:00:02.200000000Z
                        58.912164838797885\t1970-01-04T05:00:02.300000000Z
                        67.52509547112409\t1970-01-04T05:00:02.400000000Z
                        44.80468966861358\t1970-01-04T05:00:02.500000000Z
                        89.40917126581896\t1970-01-04T05:00:02.600000000Z
                        94.41658975532606\t1970-01-04T05:00:02.700000000Z
                        62.5966045857722\t1970-01-04T05:00:02.800000000Z
                        94.55893004802432\t1970-01-04T05:00:02.900000000Z
                        """,
                false
        );

        assertQuery(
                """
                        sum\tk
                        0.35983672154330515\t1970-01-04T05:00:00.000000000Z
                        76.75673070796104\t1970-01-04T05:00:00.100000000Z
                        62.173267078530984\t1970-01-04T05:00:00.200000000Z
                        63.81607531178513\t1970-01-04T05:00:00.300000000Z
                        57.93466326862211\t1970-01-04T05:00:00.400000000Z
                        12.026122412833129\t1970-01-04T05:00:00.500000000Z
                        48.820511018586934\t1970-01-04T05:00:00.600000000Z
                        26.922103479744898\t1970-01-04T05:00:00.700000000Z
                        52.98405941762054\t1970-01-04T05:00:00.800000000Z
                        84.45258177211063\t1970-01-04T05:00:00.900000000Z
                        97.5019885372507\t1970-01-04T05:00:01.000000000Z
                        49.00510449885239\t1970-01-04T05:00:01.100000000Z
                        80.01121139739173\t1970-01-04T05:00:01.200000000Z
                        92.050039469858\t1970-01-04T05:00:01.300000000Z
                        45.6344569609078\t1970-01-04T05:00:01.400000000Z
                        40.455469747939254\t1970-01-04T05:00:01.500000000Z
                        56.594291398612405\t1970-01-04T05:00:01.600000000Z
                        9.750574414434398\t1970-01-04T05:00:01.700000000Z
                        12.105630273556178\t1970-01-04T05:00:01.800000000Z
                        57.78947915182423\t1970-01-04T05:00:01.900000000Z
                        86.85154305419587\t1970-01-04T05:00:02.000000000Z
                        12.02416087573498\t1970-01-04T05:00:02.100000000Z
                        49.42890511958454\t1970-01-04T05:00:02.200000000Z
                        58.912164838797885\t1970-01-04T05:00:02.300000000Z
                        67.52509547112409\t1970-01-04T05:00:02.400000000Z
                        44.80468966861358\t1970-01-04T05:00:02.500000000Z
                        89.40917126581896\t1970-01-04T05:00:02.600000000Z
                        94.41658975532606\t1970-01-04T05:00:02.700000000Z
                        62.5966045857722\t1970-01-04T05:00:02.800000000Z
                        94.55893004802432\t1970-01-04T05:00:02.900000000Z
                        """,
                "select sum(a), k from x sample by 100T fill(none) align to calendar",
                null,
                "k",
                true,
                true
        );
    }

    @Test
    public void testSampleByNegativeTimestamp() throws Exception {
        execute("create table test ( ts TIMESTAMP_NS, value float );");
        execute("""
                insert into test VALUES
                    ('1968-10-01T01:00:00.0Z', 5),
                    ('1968-10-02T01:00:00.0Z', 10),
                    ('1968-10-03T01:00:00.0Z', 15),
                    ('1968-10-04T01:00:00.0Z', 20);""");
        assertQueryNoLeakCheck(
                """
                        ts\tavg
                        1968-10-01T00:00:00.000000000Z\t5.0
                        1968-10-02T00:00:00.000000000Z\t10.0
                        1968-10-03T00:00:00.000000000Z\t15.0
                        1968-10-04T00:00:00.000000000Z\t20.0
                        """,
                "SELECT ts, avg(value) FROM(select ts, value from test order by ts asc) sample BY 1d FILL(NULL);",
                "ts"
        );
        assertQueryNoLeakCheck(
                """
                        ts\tavg
                        1968-09-30T02:00:00.000000000Z\t5.0
                        1968-10-01T02:00:00.000000000Z\t10.0
                        1968-10-02T02:00:00.000000000Z\t15.0
                        1968-10-03T02:00:00.000000000Z\t20.0
                        """,
                "SELECT ts, avg(value) FROM(select ts, value from test order by ts asc)" +
                        " sample BY 1d FILL(NULL) ALIGN TO CALENDAR WITH OFFSET '02:00';",
                "ts"
        );
    }

    @Test
    public void testSampleByNegativeTimestampEdgeCase() throws Exception {
        execute("create table test ( ts TIMESTAMP_NS, value float );");
        execute("""
                insert into test VALUES
                    ('1969-12-31T23:00:00.0Z', 5),
                    ('1970-01-01T00:00:00.0Z', 10),
                    ('1970-01-01T01:00:00.0Z', 15),
                    ('1970-01-01T02:00:00.0Z', 20),\
                    ('1970-01-01T03:00:00.0Z', 25);""");
        assertQueryNoLeakCheck(
                """
                        ts\tavg
                        1969-12-31T00:00:00.000000000Z\t5.0
                        1970-01-01T00:00:00.000000000Z\t17.5
                        """,
                "SELECT ts, avg(value) FROM(select ts, value from test order by ts asc) sample BY 1d FILL(NULL);",
                "ts"
        );
        assertQueryNoLeakCheck(
                """
                        ts\tavg
                        1969-12-31T02:00:00.000000000Z\t10.0
                        1970-01-01T02:00:00.000000000Z\t22.5
                        """,
                "SELECT ts, avg(value) FROM(select ts, value from test order by ts asc)" +
                        " sample BY 1d FILL(NULL) ALIGN TO CALENDAR WITH OFFSET '02:00';",
                "ts"
        );
    }

    @Test
    public void testSampleByNoFillAlignToCalendarTimezoneOffset() throws Exception {
        assertQuery(
                """
                        k\tb\tc
                        1970-01-02T14:42:00.000000000Z\t\t2
                        1970-01-02T14:42:00.000000000Z\tVTJW\t1
                        1970-01-02T14:42:00.000000000Z\tRXGZ\t1
                        1970-01-02T14:42:00.000000000Z\tPEHN\t1
                        1970-01-02T16:42:00.000000000Z\t\t12
                        1970-01-02T16:42:00.000000000Z\tHYRX\t3
                        1970-01-02T16:42:00.000000000Z\tPEHN\t4
                        1970-01-02T16:42:00.000000000Z\tVTJW\t2
                        1970-01-02T16:42:00.000000000Z\tRXGZ\t1
                        1970-01-02T16:42:00.000000000Z\tCPSW\t2
                        1970-01-02T18:42:00.000000000Z\t\t12
                        1970-01-02T18:42:00.000000000Z\tVTJW\t4
                        1970-01-02T18:42:00.000000000Z\tCPSW\t4
                        1970-01-02T18:42:00.000000000Z\tHYRX\t2
                        1970-01-02T18:42:00.000000000Z\tRXGZ\t1
                        1970-01-02T18:42:00.000000000Z\tPEHN\t1
                        1970-01-02T20:42:00.000000000Z\t\t13
                        1970-01-02T20:42:00.000000000Z\tCPSW\t1
                        1970-01-02T20:42:00.000000000Z\tHYRX\t4
                        1970-01-02T20:42:00.000000000Z\tRXGZ\t2
                        1970-01-02T20:42:00.000000000Z\tVTJW\t3
                        1970-01-02T20:42:00.000000000Z\tPEHN\t1
                        1970-01-02T22:42:00.000000000Z\tRXGZ\t6
                        1970-01-02T22:42:00.000000000Z\t\t11
                        1970-01-02T22:42:00.000000000Z\tPEHN\t1
                        1970-01-02T22:42:00.000000000Z\tHYRX\t1
                        1970-01-02T22:42:00.000000000Z\tVTJW\t4
                        """,
                // correct timestamp values are 18 and 48 because 'PST' offset is negative and static offset is positive
                "select to_timezone(k, 'PST') k, b, c from (select k, b, count() c from x sample by 2h align to calendar time zone 'PST' with offset '00:42')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('1970-01-03T00:20:00.000000000Z' as timestamp_ns), 300000000000) k" +
                        " from" +
                        " long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                null,
                true,
                true
        );
    }

    @Test
    public void testSampleByNoFillNotKeyedAlignToCalendarMisalignedTimezone() throws Exception {
        // IRAN timezone is +4:30, which doesn't align well with 1hr sample
        assertQuery(
                """
                        k\tc
                        2021-03-28T04:00:00.000000000Z\t3
                        2021-03-28T05:00:00.000000000Z\t10
                        2021-03-28T06:00:00.000000000Z\t10
                        2021-03-28T07:00:00.000000000Z\t10
                        2021-03-28T08:00:00.000000000Z\t10
                        2021-03-28T09:00:00.000000000Z\t10
                        2021-03-28T10:00:00.000000000Z\t10
                        2021-03-28T11:00:00.000000000Z\t10
                        2021-03-28T12:00:00.000000000Z\t10
                        2021-03-28T13:00:00.000000000Z\t10
                        2021-03-28T14:00:00.000000000Z\t7
                        """,
                "select to_timezone(k, 'Iran') k, c from (select k, count() c from x sample by 1h align to calendar time zone 'Iran')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-03-28T00:15:00.000000000Z' as timestamp_ns), 6*60000000000) k" +
                        " from" +
                        " long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                null,
                true,
                true
        );
    }

    @Test
    public void testSampleByNoFillNotKeyedAlignToCalendarTimezone() throws Exception {
        // We are going over spring time change. Because time is "expanding" we don't have
        // to do anything special. Our UTC timestamps will show "gap" and data doesn't
        // have to change
        assertQuery(
                """
                        k\tc
                        2021-03-28T01:00:00.000000000Z\t8
                        2021-03-28T03:00:00.000000000Z\t10
                        2021-03-28T04:00:00.000000000Z\t10
                        2021-03-28T05:00:00.000000000Z\t10
                        2021-03-28T06:00:00.000000000Z\t10
                        2021-03-28T07:00:00.000000000Z\t10
                        2021-03-28T08:00:00.000000000Z\t10
                        2021-03-28T09:00:00.000000000Z\t10
                        2021-03-28T10:00:00.000000000Z\t10
                        2021-03-28T11:00:00.000000000Z\t10
                        2021-03-28T12:00:00.000000000Z\t2
                        """,
                "select to_timezone(k, 'Europe/Berlin') k, c from (select k, count() c from x sample by 1h align to calendar time zone 'Europe/Berlin')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-03-28T00:15:00.000000000Z' as timestamp_ns), 6*60000000000) k" +
                        " from long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                null,
                true,
                true
        );
    }

    @Test
    public void testSampleByNoFillNotKeyedAlignToCalendarTimezoneOct() throws Exception {
        // We are going over spring time change. Because time is "expanding" we don't have
        // to do anything special. Our UTC timestamps will show "gap" and data doesn't
        // have to change
        assertQuery(
                """
                        k\tc
                        2021-10-31T02:00:00.000000000Z\t18
                        2021-10-31T03:00:00.000000000Z\t10
                        2021-10-31T04:00:00.000000000Z\t10
                        2021-10-31T05:00:00.000000000Z\t10
                        2021-10-31T06:00:00.000000000Z\t10
                        2021-10-31T07:00:00.000000000Z\t10
                        2021-10-31T08:00:00.000000000Z\t10
                        2021-10-31T09:00:00.000000000Z\t10
                        2021-10-31T10:00:00.000000000Z\t10
                        2021-10-31T11:00:00.000000000Z\t2
                        """,
                "select to_timezone(k, 'Europe/Berlin') k, c from (select k, count() c from x sample by 1h align to calendar time zone 'Europe/Berlin')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-10-31T00:15:00.000000000Z' as timestamp_ns), 6*60000000000) k" +
                        " from" +
                        " long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                null,
                true,
                true
        );
    }

    @Test
    public void testSampleByNoFillNotKeyedAlignToCalendarTimezoneOctMin() throws Exception {
        // We are going over spring time change. Because time is "expanding" we dont have
        // to do anything special. Our UTC timestamps will show "gap" and data doesn't
        // have to change
        assertQuery(
                """
                        k\tc
                        2021-10-31T02:00:00.000000000Z\t8
                        2021-10-31T02:30:00.000000000Z\t10
                        2021-10-31T03:00:00.000000000Z\t5
                        2021-10-31T03:30:00.000000000Z\t5
                        2021-10-31T04:00:00.000000000Z\t5
                        2021-10-31T04:30:00.000000000Z\t5
                        2021-10-31T05:00:00.000000000Z\t5
                        2021-10-31T05:30:00.000000000Z\t5
                        2021-10-31T06:00:00.000000000Z\t5
                        2021-10-31T06:30:00.000000000Z\t5
                        2021-10-31T07:00:00.000000000Z\t5
                        2021-10-31T07:30:00.000000000Z\t5
                        2021-10-31T08:00:00.000000000Z\t5
                        2021-10-31T08:30:00.000000000Z\t5
                        2021-10-31T09:00:00.000000000Z\t5
                        2021-10-31T09:30:00.000000000Z\t5
                        2021-10-31T10:00:00.000000000Z\t5
                        2021-10-31T10:30:00.000000000Z\t5
                        2021-10-31T11:00:00.000000000Z\t2
                        """,
                "select to_timezone(k, 'Europe/Berlin') k, c from (select k, count() c from x sample by 30m align to calendar time zone 'Europe/Berlin')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-10-31T00:15:00.000000000Z' as timestamp_ns), 6*60000000000) k" +
                        " from" +
                        " long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                null,
                true,
                true
        );
    }

    @Test
    public void testSampleByNoFillNotKeyedAlignToCalendarTimezoneOffset() throws Exception {
        assertQuery(
                """
                        k\tc
                        1970-01-02T15:42:00.000000000Z\t15
                        1970-01-02T17:12:00.000000000Z\t18
                        1970-01-02T18:42:00.000000000Z\t18
                        1970-01-02T20:12:00.000000000Z\t18
                        1970-01-02T21:42:00.000000000Z\t18
                        1970-01-02T23:12:00.000000000Z\t13
                        """,
                "select to_timezone(k, 'PST') k, c from (select k, count() c from x sample by 90m align to calendar time zone 'PST' with offset '00:42')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 300000000000) k" +
                        " from" +
                        " long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                null,
                true,
                true
        );
    }

    @Test
    public void testSampleByNoFillNotKeyedAlignToCalendarTimezoneVariable() throws Exception {
        assertMemoryLeak(() -> {
            execute(
                    "create table x as " +
                            "(" +
                            "select" +
                            " rnd_double(0)*100 a," +
                            " rnd_symbol(5,4,4,1) b," +
                            " timestamp_sequence_ns(172800000000000, 300000000000) k" +
                            " from" +
                            " long_sequence(100)" +
                            ") timestamp(k) partition by NONE"
            );

            snapshotMemoryUsage();
            try (RecordCursorFactory factory = select("select k, count() from x sample by 90m align to calendar time zone $1 with offset $2")) {
                String expectedMoscow = """
                        k\tcount
                        1970-01-02T22:45:00.000000000Z\t3
                        1970-01-03T00:15:00.000000000Z\t18
                        1970-01-03T01:45:00.000000000Z\t18
                        1970-01-03T03:15:00.000000000Z\t18
                        1970-01-03T04:45:00.000000000Z\t18
                        1970-01-03T06:15:00.000000000Z\t18
                        1970-01-03T07:45:00.000000000Z\t7
                        """;

                String expectedPrague = """
                        k\tcount
                        1970-01-02T23:10:00.000000000Z\t8
                        1970-01-03T00:40:00.000000000Z\t18
                        1970-01-03T02:10:00.000000000Z\t18
                        1970-01-03T03:40:00.000000000Z\t18
                        1970-01-03T05:10:00.000000000Z\t18
                        1970-01-03T06:40:00.000000000Z\t18
                        1970-01-03T08:10:00.000000000Z\t2
                        """;

                sqlExecutionContext.getBindVariableService().setStr(0, "Europe/Moscow");
                sqlExecutionContext.getBindVariableService().setStr(1, "00:15");
                try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                    assertCursor(
                            expectedMoscow,
                            cursor,
                            factory.getMetadata(),
                            true
                    );
                }
                assertFactoryMemoryUsage();

                // invalid timezone
                sqlExecutionContext.getBindVariableService().setStr(0, "Oopsie");
                sqlExecutionContext.getBindVariableService().setStr(1, "00:15");
                try {
                    factory.getCursor(sqlExecutionContext);
                    Assert.fail();
                } catch (SqlException e) {
                    Assert.assertEquals(67, e.getPosition());
                    TestUtils.assertContains(e.getFlyweightMessage(), "invalid timezone: Oopsie");
                }
                assertFactoryMemoryUsage();

                sqlExecutionContext.getBindVariableService().setStr(0, "Europe/Prague");
                sqlExecutionContext.getBindVariableService().setStr(1, "uggs");
                try {
                    factory.getCursor(sqlExecutionContext);
                    Assert.fail();
                } catch (SqlException e) {
                    Assert.assertEquals(82, e.getPosition());
                    TestUtils.assertContains(e.getFlyweightMessage(), "invalid offset: uggs");
                }
                assertFactoryMemoryUsage();

                sqlExecutionContext.getBindVariableService().setStr(0, "Europe/Prague");
                sqlExecutionContext.getBindVariableService().setStr(1, "00:10");
                try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                    assertCursor(
                            expectedPrague,
                            cursor,
                            factory.getMetadata(),
                            true
                    );
                }
                assertFactoryMemoryUsage();
            }
        });
    }

    @Test
    public void testSampleByNoFillNotKeyedAlignToCalendarUTC() throws Exception {
        assertQuery(
                """
                        k\tcount
                        1970-01-03T00:00:00.000000000Z\t18
                        1970-01-03T01:30:00.000000000Z\t18
                        1970-01-03T03:00:00.000000000Z\t18
                        1970-01-03T04:30:00.000000000Z\t18
                        1970-01-03T06:00:00.000000000Z\t18
                        1970-01-03T07:30:00.000000000Z\t10
                        """,

                "select k, count() from x sample by 90m align to calendar",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 300000000000) k" +
                        " from" +
                        " long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                "k",
                true,
                true
        );
    }

    @Test
    public void testSampleByNoFillNotKeyedAlignToCalendarUTCOffset() throws Exception {
        assertQuery(
                """
                        k\tcount
                        1970-01-02T23:12:00.000000000Z\t9
                        1970-01-03T00:42:00.000000000Z\t18
                        1970-01-03T02:12:00.000000000Z\t18
                        1970-01-03T03:42:00.000000000Z\t18
                        1970-01-03T05:12:00.000000000Z\t18
                        1970-01-03T06:42:00.000000000Z\t18
                        1970-01-03T08:12:00.000000000Z\t1
                        """,
                "select k, count() from x sample by 90m align to calendar with offset '00:42'",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('1970-01-03T00:01:00.00000Z' as timestamp_ns), 300000000000) k" +
                        " from" +
                        " long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                "k",
                true,
                true
        );
    }

    @Test
    public void testSampleByRewriteJoinNoTimestamp() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table if not exists x (  ts1 timestamp_ns, ts2 timestamp_ns, sym symbol, val long ) timestamp(ts1) partition by DAY");
            assertPlanNoLeakCheck(
                    "select * from " +
                            "(select sym, first(val), avg(val), last(val), max(val) " +
                            "from x " +
                            "sample by 1m align to calendar) a " +
                            " left join " +
                            "(select sym, first(val), avg(val), last(val), max(val) " +
                            "from x " +
                            "sample by 1m align to calendar) b on(sym) ",
                    """
                            SelectedRecord
                                Hash Left Outer Join Light
                                  condition: b.sym=a.sym
                                    SelectedRecord
                                        Radix sort light
                                          keys: [ts1]
                                            Async Group By workers: 1
                                              keys: [sym,ts1]
                                              values: [first(val),avg(val),last(val),max(val)]
                                              filter: null
                                                PageFrame
                                                    Row forward scan
                                                    Frame forward scan on: x
                                    Hash
                                        SelectedRecord
                                            Radix sort light
                                              keys: [ts1]
                                                Async Group By workers: 1
                                                  keys: [sym,ts1]
                                                  values: [first(val),avg(val),last(val),max(val)]
                                                  filter: null
                                                    PageFrame
                                                        Row forward scan
                                                        Frame forward scan on: x
                            """
            );
        });
    }

    @Test
    public void testSampleByRewriteJoinTimestamp() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table if not exists x (  ts1 timestamp_ns, ts2 timestamp_ns, sym symbol, val long ) timestamp(ts1) partition by DAY");
            assertPlanNoLeakCheck(
                    "select * from " +
                            "(select ts1, sym, first(val), avg(val), last(val), max(val) " +
                            "from x " +
                            "sample by 1m align to calendar) a " +
                            " asof join " +
                            "(select ts1, sym, first(val), avg(val), last(val), max(val) " +
                            "from x " +
                            "sample by 1m align to calendar) b ",
                    """
                            SelectedRecord
                                AsOf Join
                                    Radix sort light
                                      keys: [ts1]
                                        Async Group By workers: 1
                                          keys: [ts1,sym]
                                          values: [first(val),avg(val),last(val),max(val)]
                                          filter: null
                                            PageFrame
                                                Row forward scan
                                                Frame forward scan on: x
                                    Radix sort light
                                      keys: [ts1]
                                        Async Group By workers: 1
                                          keys: [ts1,sym]
                                          values: [first(val),avg(val),last(val),max(val)]
                                          filter: null
                                            PageFrame
                                                Row forward scan
                                                Frame forward scan on: x
                            """
            );
        });
    }

    @Test
    public void testSampleByRewriteMaintainTimestamp() throws Exception {
        assertQuery("""
                        k\tsum
                        2021-03-25T18:00:00.000000000Z\t144.77803379943109
                        2021-03-26T00:00:00.000000000Z\t698.7189685053112
                        2021-03-26T06:00:00.000000000Z\t421.5252426971489
                        2021-03-26T12:00:00.000000000Z\t857.8810676038881
                        2021-03-26T18:00:00.000000000Z\t524.368651615222
                        2021-03-27T00:00:00.000000000Z\t288.92342312668086
                        2021-03-27T06:00:00.000000000Z\t678.7325602767398
                        2021-03-27T12:00:00.000000000Z\t58.67828100564961
                        2021-03-27T18:00:00.000000000Z\t762.6041348070386
                        2021-03-28T00:00:00.000000000Z\t573.4016951272048
                        2021-03-28T06:00:00.000000000Z\t410.12198091384363
                        2021-03-28T12:00:00.000000000Z\t271.0334715592426
                        2021-03-28T18:00:00.000000000Z\t315.30154566415314
                        2021-03-29T00:00:00.000000000Z\t344.7845399344325
                        2021-03-29T06:00:00.000000000Z\t736.152959556984
                        2021-03-29T12:00:00.000000000Z\t620.835997838767
                        2021-03-29T18:00:00.000000000Z\t464.7487719927086
                        2021-03-30T00:00:00.000000000Z\t200.4147829883567
                        """,
                "select k, sum(lat) from x sample by 6h  align to calendar order by k",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns('2021-03-25T23:30:00.00000000Z', 50 * 60 * 1000000000L) k" +
                        "   from" +
                        "   long_sequence(120)" +
                        ") timestamp(k)",
                "k",
                true,
                true
        );
    }

    @Test
    public void testSampleByRewriteMultipleTimestamps1() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table if not exists x (  ts1 timestamp_ns, ts2 timestamp_ns, sym symbol, val long ) timestamp(ts1) partition by DAY");
            assertPlanNoLeakCheck(
                    "select ts1 a, ts1 b, sym, first(val), avg(val), last(val), max(val) " +
                            "from x " +
                            "sample by 1m align to calendar ",
                    """
                            Radix sort light
                              keys: [b]
                                SelectedRecord
                                    Async Group By workers: 1
                                      keys: [b,sym]
                                      values: [first(val),avg(val),last(val),max(val)]
                                      filter: null
                                        PageFrame
                                            Row forward scan
                                            Frame forward scan on: x
                            """
            );
        });
    }

    @Test
    public void testSampleByRewriteMultipleTimestamps1NotKeyed() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table if not exists x (  ts1 timestamp_ns, ts2 timestamp_ns, sym symbol, val long ) timestamp(ts1) partition by DAY");
            assertPlanNoLeakCheck(
                    "select ts1 a, ts1 b, first(val), avg(val), last(val), max(val) " +
                            "from x " +
                            "sample by 1m align to calendar ",
                    """
                            Radix sort light
                              keys: [b]
                                SelectedRecord
                                    Async Group By workers: 1
                                      keys: [b]
                                      values: [first(val),avg(val),last(val),max(val)]
                                      filter: null
                                        PageFrame
                                            Row forward scan
                                            Frame forward scan on: x
                            """
            );
        });
    }

    @Test
    public void testSampleByRewriteMultipleTimestamps2() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table if not exists x (  ts1 timestamp_ns, ts2 timestamp_ns, sym symbol, val long ) timestamp(ts1) partition by DAY");
            assertPlanNoLeakCheck(
                    "select ts1 a, ts1 b, sym, first(val), avg(val), ts1 e, last(val), max(val), ts1 c, ts1 d " +
                            "from x " +
                            "sample by 1m align to calendar ",
                    """
                            Radix sort light
                              keys: [d]
                                SelectedRecord
                                    Async Group By workers: 1
                                      keys: [d,sym]
                                      values: [first(val),avg(val),last(val),max(val)]
                                      filter: null
                                        PageFrame
                                            Row forward scan
                                            Frame forward scan on: x
                            """
            );
        });
    }

    @Test
    public void testSampleByRewriteUTCOffset() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table if not exists x (  ts1 timestamp_ns, ts2 timestamp_ns, sym symbol, val long ) timestamp(ts1) partition by DAY");
            assertPlanNoLeakCheck(
                    "select ts1, sym, min(val), avg(val), max(val) " +
                            "from x " +
                            "sample by 1m align to calendar time zone 'UTC'",
                    """
                            Radix sort light
                              keys: [ts1]
                                Async Group By workers: 1
                                  keys: [ts1,sym]
                                  values: [min(val),avg(val),max(val)]
                                  filter: null
                                    PageFrame
                                        Row forward scan
                                        Frame forward scan on: x
                            """
            );
        });
    }

    @Test
    public void testSampleByRewriteUnionNoTimestamp() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table if not exists x (  ts1 timestamp_ns, ts2 timestamp_ns, sym symbol, val long ) timestamp(ts1) partition by DAY");
            assertPlanNoLeakCheck(
                    "select sym, first(val), avg(val), last(val), max(val) " +
                            "from x " +
                            "sample by 1m align to calendar " +
                            " union all " +
                            "select sym, first(val), avg(val), last(val), max(val) " +
                            "from x " +
                            "sample by 1m align to calendar ",
                    """
                            Union All
                                SelectedRecord
                                    Radix sort light
                                      keys: [ts1]
                                        Async Group By workers: 1
                                          keys: [sym,ts1]
                                          values: [first(val),avg(val),last(val),max(val)]
                                          filter: null
                                            PageFrame
                                                Row forward scan
                                                Frame forward scan on: x
                                SelectedRecord
                                    Radix sort light
                                      keys: [ts1]
                                        Async Group By workers: 1
                                          keys: [sym,ts1]
                                          values: [first(val),avg(val),last(val),max(val)]
                                          filter: null
                                            PageFrame
                                                Row forward scan
                                                Frame forward scan on: x
                            """
            );
        });
    }

    @Test
    public void testSampleByRewriteUnionTimestamp() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table if not exists x (  ts1 timestamp_ns, ts2 timestamp_ns, sym symbol, val long ) timestamp(ts1) partition by DAY");
            assertPlanNoLeakCheck(
                    "select ts1 as tstmp, sym, first(val), avg(val), last(val), max(val) " +
                            "from x " +
                            "sample by 1m align to calendar " +
                            " union all " +
                            "select ts1 as tstmp, sym, first(val), avg(val), last(val), max(val) " +
                            "from x " +
                            "sample by 1m align to calendar ",
                    """
                            Union All
                                Radix sort light
                                  keys: [tstmp]
                                    Async Group By workers: 1
                                      keys: [tstmp,sym]
                                      values: [first(val),avg(val),last(val),max(val)]
                                      filter: null
                                        PageFrame
                                            Row forward scan
                                            Frame forward scan on: x
                                Radix sort light
                                  keys: [tstmp]
                                    Async Group By workers: 1
                                      keys: [tstmp,sym]
                                      values: [first(val),avg(val),last(val),max(val)]
                                      filter: null
                                        PageFrame
                                            Row forward scan
                                            Frame forward scan on: x
                            """
            );
        });
    }

    @Test
    public void testSampleByRewriteWith() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table if not exists x (ts1 timestamp_ns, ts2 timestamp_ns, sym symbol, val long) timestamp(ts1) partition by DAY");
            assertPlanNoLeakCheck(
                    "with y as (select ts1 a, ts1 b, sym, first(val), avg(val), ts1 e, last(val), max(val), ts1 c, ts1 d " +
                            "from x " +
                            "sample by 1m align to calendar) select * from y ",
                    """
                            Radix sort light
                              keys: [d]
                                SelectedRecord
                                    Async Group By workers: 1
                                      keys: [d,sym]
                                      values: [first(val),avg(val),last(val),max(val)]
                                      filter: null
                                        PageFrame
                                            Row forward scan
                                            Frame forward scan on: x
                            """
            );
        });
    }

    @Test
    public void testSampleByRunsSequentiallyWithNonConstantFrom() throws Exception {
        assertMemoryLeak(() -> {
            execute("""
                    CREATE TABLE 'trades' (
                      symbol SYMBOL capacity 256 CACHE,
                      side SYMBOL capacity 256 CACHE,
                      price DOUBLE,
                      amount DOUBLE,
                      timestamp TIMESTAMP_NS
                    ) timestamp (timestamp) PARTITION BY DAY WAL;""");
            drainWalQueue();

            String query = """
                    select timestamp, count() from trades
                    sample by 1m FROM date_trunc('day', now()) FILL (null)\s
                    """;

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
            assertPlanNoLeakCheck(query, "Sample By\n" +
                    "  fill: null\n" +
                    "  range: (timestamp_floor('day',now()),)\n" +
                    "  values: [count(*)]\n" +
                    "    PageFrame\n" +
                    "        Row forward scan\n" +
                    "        Interval forward scan on: trades\n" +
                    "          intervals: [(\"" + formatter.format(Os.currentTimeMicros() / 1000) + "T00:00:00.000000000Z\",\"MAX\")]\n");

            assertSql("timestamp\tcount\n", query);
        });
    }

    @Test
    public void testSampleByWeekWithOffset0() throws Exception {
        assertMemoryLeak(() -> {
            execute("""
                    CREATE TABLE points ( \s
                      timestamp TIMESTAMP_NS, \s
                      value DOUBLE,
                      name SYMBOL
                    )  TIMESTAMP(timestamp)
                    PARTITION BY DAY wal
                    DEDUP UPSERT KEYS(timestamp, name);""");
            execute("INSERT INTO points VALUES " +
                    "('2025-02-01T10:00:00.000Z', 1.0, 'point')," +
                    "('2025-02-02T10:00:00.000Z', 2.0, 'point')," +
                    "('2025-02-03T10:00:00.000Z', 3.0, 'point')," +
                    "('2025-02-04T10:00:00.000Z', 4.0, 'point')," +
                    "('2025-02-05T10:00:00.000Z', 5.0, 'point')," +
                    "('2025-02-06T10:00:00.000Z', 6.0, 'point')," +
                    "('2025-02-07T10:00:00.000Z', 7.0, 'point')," +
                    "('2025-02-08T10:00:00.000Z', 8.0, 'point')," +
                    "('2025-02-09T10:00:00.000Z', 9.0, 'point')," +
                    "('2025-02-10T10:00:00.000Z', 10.0, 'point')," +
                    "('2025-02-11T10:00:00.000Z', 11.0, 'point');");
            drainWalQueue();
            assertSql("""
                            timestamp\tfirst
                            2025-01-27T00:00:00.000000000Z\t1.0
                            2025-02-03T00:00:00.000000000Z\t3.0
                            2025-02-10T00:00:00.000000000Z\t10.0
                            """,
                    """
                            SELECT timestamp, first(value)
                            FROM points
                            WHERE name = 'point'
                            SAMPLE BY 1w
                            ALIGN TO CALENDAR""");
        });
    }

    @Test
    public void testSampleByWithAsofJoin() throws Exception {
        assertMemoryLeak(() -> {
            execute("""
                    CREATE TABLE 'trades1' (
                      symbol SYMBOL capacity 256 CACHE,
                      price DOUBLE,
                      amount DOUBLE,
                      timestamp TIMESTAMP_NS
                    ) timestamp (timestamp) PARTITION BY DAY;""");
            execute("""
                    CREATE TABLE 'trades2' (
                      symbol SYMBOL capacity 256 CACHE,
                      price DOUBLE,
                      amount DOUBLE,
                      timestamp TIMESTAMP
                    ) timestamp (timestamp) PARTITION BY DAY;""");

            execute("""
                    insert into trades1\s
                    select\s
                    rnd_symbol('a', 'b', 'c'),
                    rnd_double(),
                    rnd_double(),
                    timestamp_sequence('2022-02-24', 60* 1000000L)
                    from long_sequence(10)
                    """);

            execute("""
                    insert into trades2\s
                    select\s
                    rnd_symbol('a', 'b', 'c'),
                    rnd_double(),
                    rnd_double(),
                    timestamp_sequence('2022-02-24', 60* 1000000L)
                    from long_sequence(10)
                    """);

            String expected = """
                    timestamp\tprice1\tprice2
                    2022-02-24T00:00:00.000000000Z\t0.8043224099968393\t0.24808812376657652
                    2022-02-24T00:01:00.000000000Z\t0.299199045961845\t0.4022810626779558
                    2022-02-24T00:02:00.000000000Z\t0.13123360041292131\t0.9687423276940171
                    2022-02-24T00:03:00.000000000Z\t0.22452340856088226\t0.8912587536603974
                    2022-02-24T00:04:00.000000000Z\t0.11427984775756228\t0.7883065830055033
                    2022-02-24T00:05:00.000000000Z\t0.5599161804800813\t0.5298405941762054
                    2022-02-24T00:06:00.000000000Z\t0.6276954028373309\t0.2459345277606021
                    2022-02-24T00:07:00.000000000Z\t0.3100545983862456\t0.8847591603509142
                    2022-02-24T00:08:00.000000000Z\t0.0035983672154330515\t0.7643643144642823
                    2022-02-24T00:09:00.000000000Z\t0.7675673070796104\t0.38642336707855873
                    """;

            // First, without table aliases
            assertSampleByFlavours(
                    expected,
                    """
                            SELECT trades1.timestamp,
                                  avg(trades1.price) AS price1,
                                  avg(trades2.price) AS price2
                            FROM  trades1 ASOF JOIN trades2\s
                            WHERE trades1.timestamp BETWEEN '2021-02-23T19' AND '2023-02-23T23'
                            SAMPLE BY 1s"""
            );

            // Same again, but with table aliases.
            assertSampleByFlavours(
                    expected,
                    """
                            SELECT t1.timestamp,
                                  avg(t1.price) AS price1,
                                  avg(t2.price) AS price2
                            FROM trades1 t1 ASOF JOIN trades2 t2\s
                            WHERE t1.timestamp BETWEEN '2021-02-23T19' AND '2023-02-23T23'
                            SAMPLE BY 1s"""
            );
        });
    }

    @Test
    public void testSampleByWithCTEsAndConstantKey() throws Exception {
        assertQuery(
                """
                        period_start_time\tnas_timestamp\tfeed_table\tdevice_name\tapplication_name\tapplication_group\tmin_response_time_usec\tmax_response_time_usec\ttotal_response_time_usec\tcount_response_time\tevents
                        1970-01-02T23:45:00.000000000Z\t1970-01-03T00:00:00.000000000Z\tSIP\tTJW\tHNRX\tIBBT\t754\t754\t754\t1\t1
                        1970-01-03T00:15:00.000000000Z\t1970-01-03T01:00:00.000000000Z\tSIP\tTJW\tRXP\tIBBT\t145\t145\t145\t1\t1
                        1970-01-03T00:45:00.000000000Z\t1970-01-03T02:00:00.000000000Z\tSIP\tTJW\tRXP\tZSXU\t447\t447\t447\t1\t1
                        1970-01-03T01:15:00.000000000Z\t1970-01-03T03:00:00.000000000Z\tSIP\tTJW\tRXP\tIBBT\t653\t653\t653\t1\t1
                        1970-01-03T01:45:00.000000000Z\t1970-01-03T04:00:00.000000000Z\tSIP\tTJW\tRXP\tZSXU\t991\t991\t991\t1\t1
                        1970-01-03T02:15:00.000000000Z\t1970-01-03T05:00:00.000000000Z\tSIP\tTJW\tHNRX\tZSXU\t897\t897\t897\t1\t1
                        1970-01-03T02:45:00.000000000Z\t1970-01-03T06:00:00.000000000Z\tSIP\tPSWH\tHNRX\tIBBT\t912\t912\t912\t1\t1
                        1970-01-03T03:15:00.000000000Z\t1970-01-03T07:00:00.000000000Z\tSIP\tPSWH\tHNRX\tZSXU\t769\t769\t769\t1\t1
                        1970-01-03T03:45:00.000000000Z\t1970-01-03T08:00:00.000000000Z\tSIP\tTJW\tRXP\tZSXU\t384\t384\t384\t1\t1
                        1970-01-03T04:15:00.000000000Z\t1970-01-03T09:00:00.000000000Z\tSIP\tTJW\tHNRX\tZSXU\t757\t757\t757\t1\t1
                        """,
                """
                        with srctbl as (
                          select
                              period_start_time,
                              cal_timestamp_time nas_timestamp,
                              'SIP' as feed_table,
                              device_name,
                              application_name,
                              application_group,
                              min(controlplane_response_time_usec) min_response_time_usec,
                              max(controlplane_response_time_usec) max_response_time_usec,
                              sum(controlplane_response_time_usec) total_response_time_usec,
                              count(controlplane_response_time_usec) count_response_time,
                              count() events
                          from (
                            select * from (
                            select controlplane_transaction_start_time as period_start_time, *
                            from nAS_ControlPlane_SIP
                            where not controlplane_transaction_start_time is null
                            order by 1 asc
                            ) timestamp(period_start_time)
                          ) sample by 5m align to calendar
                        )
                        select * from srctbl limit 10;""",
                "create table 'nAS_ControlPlane_SIP' as " +
                        "(" +
                        "select" +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) cal_timestamp_time," +
                        " timestamp_sequence_ns(172000000000000, 1800000000000) controlplane_transaction_start_time," +
                        " rnd_symbol(2,3,4,0) device_name," +
                        " rnd_symbol(2,3,4,0) application_name," +
                        " rnd_symbol(2,3,4,0) application_group," +
                        " rnd_long(100,1000,0) controlplane_response_time_usec" +
                        " from long_sequence(100)" +
                        ") timestamp(cal_timestamp_time) partition by hour",
                "period_start_time",
                false,
                true
        );
    }

    @Test
    public void testSampleByWithEmptyCursor() throws Exception {
        assertQuery(
                "to_timezone\ts\tlat\tlon\n",
                "select to_timezone(k, 'Europe/London'), s, lat, lon from (select k, s, first(lat) lat, last(k) lon " +
                        "from x " +
                        "where s in ('d') " +
                        "sample by 1d align to calendar time zone 'Europe/London')",
                "create table x as " +
                        "(" +
                        "select" +
                        "   rnd_double(1)*180 lat," +
                        "   rnd_double(1)*180 lon," +
                        "   rnd_symbol('a','b',null) s," +
                        "   timestamp_sequence_ns('2021-03-25T23:30:00.00000000Z', 50 * 60 * 1000000000L) k" +
                        "   from" +
                        "   long_sequence(120)" +
                        ") timestamp(k)",
                null,
                true,
                true
        );
    }

    @Test
    public void testSampleByWithFilterAndOrderByAndLimit() throws Exception {
        assertQuery(
                """
                        open\thigh\tlow\tclose\tvolume\ttimestamp
                        22.463013424972587\t90.75843364017028\t16.381374773748515\t75.88175403454873\t440.2232295756601\t1970-01-03T00:00:00.000000000Z
                        """,
                "select * from (" +
                        "  select" +
                        "    first(price) AS open," +
                        "    max(price) AS high," +
                        "    min(price) AS low," +
                        "    last(price) AS close," +
                        "    sum(amount) AS volume," +
                        "    created_at as timestamp" +
                        "  from trades" +
                        "  where market_id = 'btcusdt' AND created_at > dateadd('m', -60, 172800000000)" +
                        "  sample by 60m" +
                        "  fill(null, null, null, null, 0) align to calendar" +
                        ") order by timestamp desc limit 0, 1",
                "create table trades as " +
                        "(" +
                        "select" +
                        " rnd_str('btcusdt', 'ethusdt') market_id," +
                        " rnd_double(0) * 100 price," +
                        " rnd_double(0) * 100 amount," +
                        " timestamp_sequence_ns(172800000000000, 3600000000) created_at" +
                        " from long_sequence(20)" +
                        ") timestamp(created_at) partition by day",
                "timestamp###DESC",
                true,
                true
        );

        assertQuery(
                """
                        open\thigh\tlow\tclose\tvolume\ttimestamp
                        65.51335839796312\t94.55893004802432\t18.336217509438512\t77.0079809007092\t519.2795145577336\t1970-01-03T00:00:00.000000000Z
                        """,
                "select * from (" +
                        "  select" +
                        "    first(price) AS open," +
                        "    max(price) AS high," +
                        "    min(price) AS low," +
                        "    last(price) AS close," +
                        "    sum(amount) AS volume," +
                        "    created_at as timestamp" +
                        "  from trades_varchar" +
                        "  where market_id = 'btcusdt' AND created_at > dateadd('m', -60, 172800000000)" +
                        "  sample by 60m" +
                        "  fill(null, null, null, null, 0) align to calendar" +
                        ") order by timestamp desc limit 0, 1",
                "create table trades_varchar as " +
                        "(" +
                        "select" +
                        " rnd_varchar('btcusdt', 'ethusdt') market_id," +
                        " rnd_double(0) * 100 price," +
                        " rnd_double(0) * 100 amount," +
                        " timestamp_sequence_ns(172800000000000, 3600000000) created_at" +
                        " from long_sequence(20)" +
                        ") timestamp(created_at) partition by day",
                "timestamp###DESC",
                true,
                true
        );
    }

    @Test
    public void testSampleByWithFullDoesNotReferenceMutableCharSequence() throws Exception {
        assertMemoryLeak(() -> {
            execute(FROM_TO_DDL);

            String expected = """
                    ts\tavg
                    2000-01-01T00:00:00.000000000Z\tnull
                    2004-01-01T00:00:00.000000000Z\tnull
                    2008-01-01T00:00:00.000000000Z\tnull
                    2012-01-01T00:00:00.000000000Z\tnull
                    2016-01-01T00:00:00.000000000Z\t240.5
                    2020-01-01T00:00:00.000000000Z\tnull
                    2024-01-01T00:00:00.000000000Z\tnull
                    2028-01-01T00:00:00.000000000Z\tnull
                    2032-01-01T00:00:00.000000000Z\tnull
                    2036-01-01T00:00:00.000000000Z\tnull
                    2040-01-01T00:00:00.000000000Z\tnull
                    2044-01-01T00:00:00.000000000Z\tnull
                    2048-01-01T00:00:00.000000000Z\tnull
                    """;
            StringSink sql = new StringSink();
            sql.put("select ts, avg(x) from fromto sample by 4y from '2000-01-01' to '2050-01-01' fill(null)");
            try (SqlCompiler compiler = engine.getSqlCompiler();
                 RecordCursorFactory factory = compiler.compile(sql, sqlExecutionContext).getRecordCursorFactory()
            ) {
                sql.clear();
                sql.put("just random rubbish to make sure the sql string sink overwrites its previous content");
                try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                    RecordMetadata metadata = factory.getMetadata();
                    sink.clear();
                    CursorPrinter.println(metadata, sink);

                    final Record record = cursor.getRecord();
                    while (cursor.hasNext()) {
                        TestUtils.println(record, metadata, sink);
                    }
                }
                assertEquals(expected, sink);
            }
        });
    }

    @Test
    public void testSampleByWithOrderByDescTimestamp() throws Exception {
        assertQuery(
                """
                        created_at\tfirst
                        1970-01-01T08:00:00.000000000Z\t42.17768841969397
                        1970-01-01T06:00:00.000000000Z\t34.91070363730514
                        1970-01-01T04:00:00.000000000Z\t79.05675319675964
                        1970-01-01T02:00:00.000000000Z\t8.43832076262595
                        1970-01-01T00:00:00.000000000Z\t80.43224099968394
                        """,
                "select created_at, first(price)" +
                        " from trades" +
                        " sample by 2h align to first observation" +
                        " order by created_at desc",
                "create table trades as " +
                        "(" +
                        "select" +
                        " rnd_double(0) * 100 price," +
                        " timestamp_sequence_ns(0, 3600000000000) created_at" + // 1 hour step
                        " from long_sequence(10)" + // 10 rows
                        ") timestamp(created_at) partition by day",
                "created_at###DESC",
                true,
                false
        );
    }

    @Test
    public void testSampleByWithPredicate() throws Exception {
        assertMemoryLeak(() -> {
            execute("""
                    create table tab as (
                    select dateadd('m', 11*x::int, '2022-12-01T01:00:00.000000000Z') ts, x v, rnd_str('A', 'B') s
                    from long_sequence(6) ) timestamp(ts)""");

            assertQueryNoLeakCheck("""
                            ts\tv\ts
                            2022-12-01T01:11:00.000000000Z\t1\tA
                            2022-12-01T01:22:00.000000000Z\t2\tA
                            2022-12-01T01:33:00.000000000Z\t3\tB
                            2022-12-01T01:44:00.000000000Z\t4\tB
                            2022-12-01T01:55:00.000000000Z\t5\tB
                            2022-12-01T02:06:00.000000000Z\t6\tB
                            """,
                    "select * from tab", "ts", true, true
            );

            String query = "select ts, s, first(v) from tab where s = 'B' and ts > '2022-12-01T00:00:00.000000000Z' sample by 30m fill(prev) align to first observation";

            assertPlanNoLeakCheck(
                    query,
                    """
                            Sample By
                              fill: prev
                              keys: [ts,s]
                              values: [first(v)]
                                Async Filter workers: 1
                                  filter: s='B'
                                    PageFrame
                                        Row forward scan
                                        Interval forward scan on: tab
                                          intervals: [("2022-12-01T00:00:00.000000001Z","MAX")]
                            """
            );

            assertQueryNoLeakCheck(
                    """
                            ts\ts\tfirst
                            2022-12-01T01:33:00.000000000Z\tB\t3
                            2022-12-01T02:03:00.000000000Z\tB\t6
                            """,
                    query,
                    "ts",
                    false
            );
        });
    }

    @Test
    public void testSampleByWithProjection() throws Exception {
        assertMemoryLeak(() -> {
            execute("""
                    CREATE TABLE 'trades' (
                      symbol SYMBOL capacity 256 CACHE,
                      price DOUBLE,
                      amount DOUBLE,
                      timestamp TIMESTAMP_NS
                    ) timestamp (timestamp) PARTITION BY DAY;""");
            execute("""
                    insert into trades\s
                    select\s
                    rnd_symbol('a', 'b', 'c'),
                    rnd_double(),
                    rnd_double(),
                    timestamp_sequence('2022-02-24', 60* 1000000L)
                    from long_sequence(10)
                    """);

            assertSampleByFlavours(
                    """
                            symbol\tsum\tvwap\tNYTime
                            a\t0.6390492980774742\t0.3421677972133922\t2022-02-23T19:00:00.000000000Z
                            c\t0.20447441837877756\t0.299199045961845\t2022-02-23T19:00:00.000000000Z
                            b\t1.2527510748803818\t0.12497877004395191\t2022-02-23T19:00:00.000000000Z
                            b\t0.42215759939956354\t0.33181055449773833\t2022-02-23T19:05:00.000000000Z
                            c\t2.1714261356369606\t0.5397631964717502\t2022-02-23T19:05:00.000000000Z
                            """,
                    """
                            select symbol,sum(amount), vwap(price, amount), to_timezone(timestamp,'EST') NYTime
                            from trades
                            sample by 5m"""
            );

            assertSampleByFlavours("""
                            symbol\tsum\tvwap\tNYTime
                            a\t0.6390492980774742\t0.3421677972133922\t2022-02-23T21:00:00.000000000Z
                            c\t0.20447441837877756\t0.299199045961845\t2022-02-23T21:00:00.000000000Z
                            b\t1.2527510748803818\t0.12497877004395191\t2022-02-23T21:00:00.000000000Z
                            b\t0.42215759939956354\t0.33181055449773833\t2022-02-23T21:05:00.000000000Z
                            c\t2.1714261356369606\t0.5397631964717502\t2022-02-23T21:05:00.000000000Z
                            """,
                    """
                            select symbol,sum(amount), vwap(price, amount), dateadd('h', 2, to_timezone(timestamp,'EST')) NYTime
                            from trades
                            sample by 5m""");

            assertSampleByFlavours(
                    """
                            symbol\tsum\tvwap\tNYTime
                            a\t0.6390492980774742\t0.3421677972133922\t2022-02-23T21:00:01.000000000Z
                            c\t0.20447441837877756\t0.299199045961845\t2022-02-23T21:00:01.000000000Z
                            b\t1.2527510748803818\t0.12497877004395191\t2022-02-23T21:00:01.000000000Z
                            b\t0.42215759939956354\t0.33181055449773833\t2022-02-23T21:05:01.000000000Z
                            c\t2.1714261356369606\t0.5397631964717502\t2022-02-23T21:05:01.000000000Z
                            """,
                    """
                            select symbol,sum(amount), vwap(price, amount), dateadd('h', 2, to_timezone(timestamp,'EST')) + 1000000000L NYTime
                            from trades
                            sample by 5m"""
            );

            assertSampleByFlavours(
                    """
                            symbol\tsum\tvwap\tNYTime
                            aabcd\t0.6390492980774742\t0.3421677972133922\t2022-02-23T21:00:01.000000000Z
                            cabcd\t0.20447441837877756\t0.299199045961845\t2022-02-23T21:00:01.000000000Z
                            babcd\t1.2527510748803818\t0.12497877004395191\t2022-02-23T21:00:01.000000000Z
                            babcd\t0.42215759939956354\t0.33181055449773833\t2022-02-23T21:05:01.000000000Z
                            cabcd\t2.1714261356369606\t0.5397631964717502\t2022-02-23T21:05:01.000000000Z
                            """,
                    """
                            select symbol || 'abcd' as symbol, sum(amount), vwap(price, amount), dateadd('h', 2, to_timezone(timestamp,'EST')) + 1000000000L NYTime
                            from trades
                            sample by 5m"""
            );

            assertSampleByFlavours(
                    """
                            symbol\tsum\tprice\tlast_timestamp
                            a\t0.6390492980774742\t0.22452340856088226\t2022-02-24T00:03:00.000000000Z
                            c\t2.3759005540157383\t0.7675673070796104\t2022-02-24T00:09:00.000000000Z
                            b\t1.6749086742799453\t0.3100545983862456\t2022-02-24T00:07:00.000000000Z
                            """,
                    """
                            select symbol, sum(amount), last(price) price, last(timestamp) last_timestamp
                            from trades
                            sample by 1h"""
            );

            assertSampleByFlavours(
                    """
                            symbol\tsum\tprice\ttimestamp\tlast_timestamp
                            a\t0.6390492980774742\t0.22452340856088226\t2022-02-24T00:00:00.000000000Z\t2022-02-24T00:03:00.000000000Z
                            c\t2.3759005540157383\t0.7675673070796104\t2022-02-24T00:00:00.000000000Z\t2022-02-24T00:09:00.000000000Z
                            b\t1.6749086742799453\t0.3100545983862456\t2022-02-24T00:00:00.000000000Z\t2022-02-24T00:07:00.000000000Z
                            """,
                    """
                            select symbol, sum(amount), last(price) price, timestamp, max(timestamp) last_timestamp
                            from trades
                            sample by 1d"""
            );
        });
    }

    @Test
    public void testSampleByWithSubQueryAndFromToNoFill() throws Exception {
        assertMemoryLeak(() -> {
            execute(FROM_TO_DDL);
            drainWalQueue();
            assertSql(
                    """
                            ts1\tavg
                            2017-12-30T00:00:00.000000000Z\t72.5
                            2018-01-04T00:00:00.000000000Z\t264.5
                            2018-01-09T00:00:00.000000000Z\t432.5
                            """,
                    "select ts1, avg(x) from (select ts as ts1, x from fromto where x > 0)\n" +
                            "sample by 5d from '2017-12-20'"
            );
        });
    }

    @Test
    public void testSampleCountFillLinear() throws Exception {
        assertQuery(
                """
                        b\tcount\tk
                        \t15\t1970-01-03T02:00:00.000000000Z
                        VTJW\t3\t1970-01-03T02:00:00.000000000Z
                        RXGZ\t2\t1970-01-03T02:00:00.000000000Z
                        PEHN\t5\t1970-01-03T02:00:00.000000000Z
                        HYRX\t3\t1970-01-03T02:00:00.000000000Z
                        CPSW\t2\t1970-01-03T02:00:00.000000000Z
                        \t14\t1970-01-03T05:00:00.000000000Z
                        VTJW\t4\t1970-01-03T05:00:00.000000000Z
                        CPSW\t5\t1970-01-03T05:00:00.000000000Z
                        HYRX\t4\t1970-01-03T05:00:00.000000000Z
                        RXGZ\t2\t1970-01-03T05:00:00.000000000Z
                        PEHN\t1\t1970-01-03T05:00:00.000000000Z
                        \t17\t1970-01-03T08:00:00.000000000Z
                        VTJW\t4\t1970-01-03T08:00:00.000000000Z
                        HYRX\t3\t1970-01-03T08:00:00.000000000Z
                        RXGZ\t4\t1970-01-03T08:00:00.000000000Z
                        PEHN\t2\t1970-01-03T08:00:00.000000000Z
                        CPSW\t8\t1970-01-03T08:00:00.000000000Z
                        \t4\t1970-01-03T11:00:00.000000000Z
                        RXGZ\t3\t1970-01-03T11:00:00.000000000Z
                        VTJW\t3\t1970-01-03T11:00:00.000000000Z
                        PEHN\t3\t1970-01-03T11:00:00.000000000Z
                        HYRX\t2\t1970-01-03T11:00:00.000000000Z
                        CPSW\t11\t1970-01-03T11:00:00.000000000Z
                        """,
                "select b, count(), k from x sample by 3h fill(linear) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns('1970-01-03T02:00:00.000000000Z', 360000000000) k" +
                        " from" +
                        " long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence('1970-01-03T13:10:00.000000000Z', 360000000) k" +
                        " from" +
                        " long_sequence(35)" +
                        ") timestamp(k)",
                """
                        b\tcount\tk
                        \t15\t1970-01-03T02:00:00.000000000Z
                        VTJW\t3\t1970-01-03T02:00:00.000000000Z
                        RXGZ\t2\t1970-01-03T02:00:00.000000000Z
                        PEHN\t5\t1970-01-03T02:00:00.000000000Z
                        HYRX\t3\t1970-01-03T02:00:00.000000000Z
                        CPSW\t2\t1970-01-03T02:00:00.000000000Z
                        CGFN\t-2\t1970-01-03T02:00:00.000000000Z
                        NPIW\t1\t1970-01-03T02:00:00.000000000Z
                        PEVM\t-2\t1970-01-03T02:00:00.000000000Z
                        WGRM\tnull\t1970-01-03T02:00:00.000000000Z
                        ZNFK\tnull\t1970-01-03T02:00:00.000000000Z
                        \t14\t1970-01-03T05:00:00.000000000Z
                        VTJW\t4\t1970-01-03T05:00:00.000000000Z
                        CPSW\t5\t1970-01-03T05:00:00.000000000Z
                        HYRX\t4\t1970-01-03T05:00:00.000000000Z
                        RXGZ\t2\t1970-01-03T05:00:00.000000000Z
                        PEHN\t1\t1970-01-03T05:00:00.000000000Z
                        CGFN\t-1\t1970-01-03T05:00:00.000000000Z
                        NPIW\t1\t1970-01-03T05:00:00.000000000Z
                        PEVM\t-1\t1970-01-03T05:00:00.000000000Z
                        WGRM\tnull\t1970-01-03T05:00:00.000000000Z
                        ZNFK\tnull\t1970-01-03T05:00:00.000000000Z
                        \t17\t1970-01-03T08:00:00.000000000Z
                        VTJW\t4\t1970-01-03T08:00:00.000000000Z
                        HYRX\t3\t1970-01-03T08:00:00.000000000Z
                        RXGZ\t4\t1970-01-03T08:00:00.000000000Z
                        PEHN\t2\t1970-01-03T08:00:00.000000000Z
                        CPSW\t8\t1970-01-03T08:00:00.000000000Z
                        CGFN\t0\t1970-01-03T08:00:00.000000000Z
                        NPIW\t1\t1970-01-03T08:00:00.000000000Z
                        PEVM\t0\t1970-01-03T08:00:00.000000000Z
                        WGRM\tnull\t1970-01-03T08:00:00.000000000Z
                        ZNFK\tnull\t1970-01-03T08:00:00.000000000Z
                        \t10\t1970-01-03T11:00:00.000000000Z
                        RXGZ\t3\t1970-01-03T11:00:00.000000000Z
                        VTJW\t3\t1970-01-03T11:00:00.000000000Z
                        CGFN\t1\t1970-01-03T11:00:00.000000000Z
                        NPIW\t1\t1970-01-03T11:00:00.000000000Z
                        PEVM\t1\t1970-01-03T11:00:00.000000000Z
                        PEHN\t3\t1970-01-03T11:00:00.000000000Z
                        HYRX\t2\t1970-01-03T11:00:00.000000000Z
                        CPSW\t11\t1970-01-03T11:00:00.000000000Z
                        WGRM\tnull\t1970-01-03T11:00:00.000000000Z
                        ZNFK\tnull\t1970-01-03T11:00:00.000000000Z
                        \t15\t1970-01-03T14:00:00.000000000Z
                        WGRM\t3\t1970-01-03T14:00:00.000000000Z
                        CGFN\t2\t1970-01-03T14:00:00.000000000Z
                        PEVM\t2\t1970-01-03T14:00:00.000000000Z
                        ZNFK\t3\t1970-01-03T14:00:00.000000000Z
                        NPIW\t1\t1970-01-03T14:00:00.000000000Z
                        VTJW\t2\t1970-01-03T14:00:00.000000000Z
                        RXGZ\t2\t1970-01-03T14:00:00.000000000Z
                        PEHN\t4\t1970-01-03T14:00:00.000000000Z
                        HYRX\t1\t1970-01-03T14:00:00.000000000Z
                        CPSW\t14\t1970-01-03T14:00:00.000000000Z
                        """,
                true,
                true,
                false
        );

        execute("drop table x");

        assertQuery(
                """
                        b\tcount\tk
                        \t6\t1970-01-03T00:00:00.000000000Z
                        NLRH\t2\t1970-01-03T00:00:00.000000000Z
                        PFYX\t1\t1970-01-03T00:00:00.000000000Z
                        WQXY\t1\t1970-01-03T00:00:00.000000000Z
                        PVKN\t1\t1970-01-03T00:00:00.000000000Z
                        CBWL\t2\t1970-01-03T00:00:00.000000000Z
                        NLRH\t4\t1970-01-03T03:00:00.000000000Z
                        \t14\t1970-01-03T03:00:00.000000000Z
                        PVKN\t3\t1970-01-03T03:00:00.000000000Z
                        CBWL\t4\t1970-01-03T03:00:00.000000000Z
                        WQXY\t3\t1970-01-03T03:00:00.000000000Z
                        PFYX\t2\t1970-01-03T03:00:00.000000000Z
                        \t13\t1970-01-03T06:00:00.000000000Z
                        NLRH\t4\t1970-01-03T06:00:00.000000000Z
                        PVKN\t5\t1970-01-03T06:00:00.000000000Z
                        PFYX\t4\t1970-01-03T06:00:00.000000000Z
                        WQXY\t4\t1970-01-03T06:00:00.000000000Z
                        CBWL\t5\t1970-01-03T06:00:00.000000000Z
                        \t15\t1970-01-03T09:00:00.000000000Z
                        PFYX\t4\t1970-01-03T09:00:00.000000000Z
                        WQXY\t2\t1970-01-03T09:00:00.000000000Z
                        CBWL\t7\t1970-01-03T09:00:00.000000000Z
                        NLRH\t2\t1970-01-03T09:00:00.000000000Z
                        PVKN\t7\t1970-01-03T09:00:00.000000000Z
                        """,
                "select b, count(), k from x sample by 3h fill(linear) align to calendar",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns('1970-01-03T02:00:00.000000000Z', 360000000000) k" +
                        " from" +
                        " long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(cast('1970-01-03T13:10:00.000000000Z' as timestamp), 360000000) k" +
                        " from" +
                        " long_sequence(35)" +
                        ") timestamp(k)",
                """
                        b\tcount\tk
                        \t6\t1970-01-03T00:00:00.000000000Z
                        NLRH\t2\t1970-01-03T00:00:00.000000000Z
                        PFYX\t1\t1970-01-03T00:00:00.000000000Z
                        WQXY\t1\t1970-01-03T00:00:00.000000000Z
                        PVKN\t1\t1970-01-03T00:00:00.000000000Z
                        CBWL\t2\t1970-01-03T00:00:00.000000000Z
                        QWPK\tnull\t1970-01-03T00:00:00.000000000Z
                        ONNL\t2\t1970-01-03T00:00:00.000000000Z
                        ITWG\t7\t1970-01-03T00:00:00.000000000Z
                        LFCY\t1\t1970-01-03T00:00:00.000000000Z
                        LHTI\tnull\t1970-01-03T00:00:00.000000000Z
                        NLRH\t4\t1970-01-03T03:00:00.000000000Z
                        \t14\t1970-01-03T03:00:00.000000000Z
                        PVKN\t3\t1970-01-03T03:00:00.000000000Z
                        CBWL\t4\t1970-01-03T03:00:00.000000000Z
                        WQXY\t3\t1970-01-03T03:00:00.000000000Z
                        PFYX\t2\t1970-01-03T03:00:00.000000000Z
                        QWPK\tnull\t1970-01-03T03:00:00.000000000Z
                        ONNL\t2\t1970-01-03T03:00:00.000000000Z
                        ITWG\t6\t1970-01-03T03:00:00.000000000Z
                        LFCY\t1\t1970-01-03T03:00:00.000000000Z
                        LHTI\tnull\t1970-01-03T03:00:00.000000000Z
                        \t13\t1970-01-03T06:00:00.000000000Z
                        NLRH\t4\t1970-01-03T06:00:00.000000000Z
                        PVKN\t5\t1970-01-03T06:00:00.000000000Z
                        PFYX\t4\t1970-01-03T06:00:00.000000000Z
                        WQXY\t4\t1970-01-03T06:00:00.000000000Z
                        CBWL\t5\t1970-01-03T06:00:00.000000000Z
                        QWPK\tnull\t1970-01-03T06:00:00.000000000Z
                        ONNL\t2\t1970-01-03T06:00:00.000000000Z
                        ITWG\t5\t1970-01-03T06:00:00.000000000Z
                        LFCY\t1\t1970-01-03T06:00:00.000000000Z
                        LHTI\tnull\t1970-01-03T06:00:00.000000000Z
                        \t15\t1970-01-03T09:00:00.000000000Z
                        PFYX\t4\t1970-01-03T09:00:00.000000000Z
                        WQXY\t2\t1970-01-03T09:00:00.000000000Z
                        CBWL\t7\t1970-01-03T09:00:00.000000000Z
                        NLRH\t2\t1970-01-03T09:00:00.000000000Z
                        PVKN\t7\t1970-01-03T09:00:00.000000000Z
                        QWPK\tnull\t1970-01-03T09:00:00.000000000Z
                        ONNL\t2\t1970-01-03T09:00:00.000000000Z
                        ITWG\t4\t1970-01-03T09:00:00.000000000Z
                        LFCY\t1\t1970-01-03T09:00:00.000000000Z
                        LHTI\tnull\t1970-01-03T09:00:00.000000000Z
                        QWPK\t1\t1970-01-03T12:00:00.000000000Z
                        \t12\t1970-01-03T12:00:00.000000000Z
                        ONNL\t2\t1970-01-03T12:00:00.000000000Z
                        ITWG\t3\t1970-01-03T12:00:00.000000000Z
                        LFCY\t1\t1970-01-03T12:00:00.000000000Z
                        NLRH\t0\t1970-01-03T12:00:00.000000000Z
                        PFYX\t4\t1970-01-03T12:00:00.000000000Z
                        WQXY\t0\t1970-01-03T12:00:00.000000000Z
                        PVKN\t9\t1970-01-03T12:00:00.000000000Z
                        CBWL\t9\t1970-01-03T12:00:00.000000000Z
                        LHTI\tnull\t1970-01-03T12:00:00.000000000Z
                        \t10\t1970-01-03T15:00:00.000000000Z
                        ONNL\t2\t1970-01-03T15:00:00.000000000Z
                        LHTI\t1\t1970-01-03T15:00:00.000000000Z
                        LFCY\t1\t1970-01-03T15:00:00.000000000Z
                        ITWG\t2\t1970-01-03T15:00:00.000000000Z
                        NLRH\t-2\t1970-01-03T15:00:00.000000000Z
                        PFYX\t4\t1970-01-03T15:00:00.000000000Z
                        WQXY\t-2\t1970-01-03T15:00:00.000000000Z
                        PVKN\t11\t1970-01-03T15:00:00.000000000Z
                        CBWL\t11\t1970-01-03T15:00:00.000000000Z
                        QWPK\tnull\t1970-01-03T15:00:00.000000000Z
                        """,
                true,
                true,
                false
        );
    }

    @Test
    public void testSampleCountFillLinearFromSubQuery() throws Exception {
        assertQuery(
                """
                        b\tcount\tk
                        CPSW\t1\t1970-01-03T05:24:00.000000000Z
                        PEHN\t1\t1970-01-03T05:24:00.000000000Z
                        HYRX\t1\t1970-01-03T05:24:00.000000000Z
                        VTJW\tnull\t1970-01-03T05:24:00.000000000Z
                        RXGZ\tnull\t1970-01-03T05:24:00.000000000Z
                        \tnull\t1970-01-03T05:24:00.000000000Z
                        VTJW\t1\t1970-01-03T08:24:00.000000000Z
                        RXGZ\t1\t1970-01-03T08:24:00.000000000Z
                        \t1\t1970-01-03T08:24:00.000000000Z
                        CPSW\tnull\t1970-01-03T08:24:00.000000000Z
                        PEHN\tnull\t1970-01-03T08:24:00.000000000Z
                        HYRX\tnull\t1970-01-03T08:24:00.000000000Z
                        """,
                "select b, count(), k from (x latest on k partition by b) sample by 3h fill(linear) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 360000000000) k" +
                        " from" +
                        " long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 360000000) k" +
                        " from" +
                        " long_sequence(35)" +
                        ") timestamp(k)",
                """
                        b\tcount\tk
                        CPSW\t1\t1970-01-03T05:24:00.000000000Z
                        PEHN\t1\t1970-01-03T05:24:00.000000000Z
                        HYRX\t1\t1970-01-03T05:24:00.000000000Z
                        VTJW\tnull\t1970-01-03T05:24:00.000000000Z
                        RXGZ\tnull\t1970-01-03T05:24:00.000000000Z
                        WGRM\tnull\t1970-01-03T05:24:00.000000000Z
                        NPIW\tnull\t1970-01-03T05:24:00.000000000Z
                        CGFN\tnull\t1970-01-03T05:24:00.000000000Z
                        ZNFK\tnull\t1970-01-03T05:24:00.000000000Z
                        PEVM\tnull\t1970-01-03T05:24:00.000000000Z
                        \tnull\t1970-01-03T05:24:00.000000000Z
                        VTJW\t1\t1970-01-03T08:24:00.000000000Z
                        RXGZ\t1\t1970-01-03T08:24:00.000000000Z
                        CPSW\tnull\t1970-01-03T08:24:00.000000000Z
                        PEHN\tnull\t1970-01-03T08:24:00.000000000Z
                        HYRX\tnull\t1970-01-03T08:24:00.000000000Z
                        WGRM\tnull\t1970-01-03T08:24:00.000000000Z
                        NPIW\tnull\t1970-01-03T08:24:00.000000000Z
                        CGFN\tnull\t1970-01-03T08:24:00.000000000Z
                        ZNFK\tnull\t1970-01-03T08:24:00.000000000Z
                        PEVM\tnull\t1970-01-03T08:24:00.000000000Z
                        \tnull\t1970-01-03T08:24:00.000000000Z
                        CPSW\tnull\t1970-01-03T11:24:00.000000000Z
                        PEHN\tnull\t1970-01-03T11:24:00.000000000Z
                        HYRX\tnull\t1970-01-03T11:24:00.000000000Z
                        VTJW\tnull\t1970-01-03T11:24:00.000000000Z
                        RXGZ\tnull\t1970-01-03T11:24:00.000000000Z
                        WGRM\tnull\t1970-01-03T11:24:00.000000000Z
                        NPIW\tnull\t1970-01-03T11:24:00.000000000Z
                        CGFN\tnull\t1970-01-03T11:24:00.000000000Z
                        ZNFK\tnull\t1970-01-03T11:24:00.000000000Z
                        PEVM\tnull\t1970-01-03T11:24:00.000000000Z
                        \tnull\t1970-01-03T11:24:00.000000000Z
                        CPSW\tnull\t1970-01-03T14:24:00.000000000Z
                        PEHN\tnull\t1970-01-03T14:24:00.000000000Z
                        HYRX\tnull\t1970-01-03T14:24:00.000000000Z
                        VTJW\tnull\t1970-01-03T14:24:00.000000000Z
                        RXGZ\tnull\t1970-01-03T14:24:00.000000000Z
                        WGRM\tnull\t1970-01-03T14:24:00.000000000Z
                        NPIW\tnull\t1970-01-03T14:24:00.000000000Z
                        CGFN\tnull\t1970-01-03T14:24:00.000000000Z
                        ZNFK\tnull\t1970-01-03T14:24:00.000000000Z
                        PEVM\tnull\t1970-01-03T14:24:00.000000000Z
                        \tnull\t1970-01-03T14:24:00.000000000Z
                        CPSW\tnull\t1970-01-03T17:24:00.000000000Z
                        PEHN\tnull\t1970-01-03T17:24:00.000000000Z
                        HYRX\tnull\t1970-01-03T17:24:00.000000000Z
                        VTJW\tnull\t1970-01-03T17:24:00.000000000Z
                        RXGZ\tnull\t1970-01-03T17:24:00.000000000Z
                        WGRM\tnull\t1970-01-03T17:24:00.000000000Z
                        NPIW\tnull\t1970-01-03T17:24:00.000000000Z
                        CGFN\tnull\t1970-01-03T17:24:00.000000000Z
                        ZNFK\tnull\t1970-01-03T17:24:00.000000000Z
                        PEVM\tnull\t1970-01-03T17:24:00.000000000Z
                        \tnull\t1970-01-03T17:24:00.000000000Z
                        CPSW\tnull\t1970-01-03T20:24:00.000000000Z
                        PEHN\tnull\t1970-01-03T20:24:00.000000000Z
                        HYRX\tnull\t1970-01-03T20:24:00.000000000Z
                        VTJW\tnull\t1970-01-03T20:24:00.000000000Z
                        RXGZ\tnull\t1970-01-03T20:24:00.000000000Z
                        WGRM\tnull\t1970-01-03T20:24:00.000000000Z
                        NPIW\tnull\t1970-01-03T20:24:00.000000000Z
                        CGFN\tnull\t1970-01-03T20:24:00.000000000Z
                        ZNFK\tnull\t1970-01-03T20:24:00.000000000Z
                        PEVM\tnull\t1970-01-03T20:24:00.000000000Z
                        \tnull\t1970-01-03T20:24:00.000000000Z
                        CPSW\tnull\t1970-01-03T23:24:00.000000000Z
                        PEHN\tnull\t1970-01-03T23:24:00.000000000Z
                        HYRX\tnull\t1970-01-03T23:24:00.000000000Z
                        VTJW\tnull\t1970-01-03T23:24:00.000000000Z
                        RXGZ\tnull\t1970-01-03T23:24:00.000000000Z
                        WGRM\tnull\t1970-01-03T23:24:00.000000000Z
                        NPIW\tnull\t1970-01-03T23:24:00.000000000Z
                        CGFN\tnull\t1970-01-03T23:24:00.000000000Z
                        ZNFK\tnull\t1970-01-03T23:24:00.000000000Z
                        PEVM\tnull\t1970-01-03T23:24:00.000000000Z
                        \tnull\t1970-01-03T23:24:00.000000000Z
                        CPSW\tnull\t1970-01-04T02:24:00.000000000Z
                        PEHN\tnull\t1970-01-04T02:24:00.000000000Z
                        HYRX\tnull\t1970-01-04T02:24:00.000000000Z
                        VTJW\tnull\t1970-01-04T02:24:00.000000000Z
                        RXGZ\tnull\t1970-01-04T02:24:00.000000000Z
                        WGRM\tnull\t1970-01-04T02:24:00.000000000Z
                        NPIW\tnull\t1970-01-04T02:24:00.000000000Z
                        CGFN\tnull\t1970-01-04T02:24:00.000000000Z
                        ZNFK\tnull\t1970-01-04T02:24:00.000000000Z
                        PEVM\tnull\t1970-01-04T02:24:00.000000000Z
                        \tnull\t1970-01-04T02:24:00.000000000Z
                        WGRM\t1\t1970-01-04T05:24:00.000000000Z
                        NPIW\t1\t1970-01-04T05:24:00.000000000Z
                        CGFN\t1\t1970-01-04T05:24:00.000000000Z
                        ZNFK\t1\t1970-01-04T05:24:00.000000000Z
                        PEVM\t1\t1970-01-04T05:24:00.000000000Z
                        CPSW\tnull\t1970-01-04T05:24:00.000000000Z
                        PEHN\tnull\t1970-01-04T05:24:00.000000000Z
                        HYRX\tnull\t1970-01-04T05:24:00.000000000Z
                        VTJW\tnull\t1970-01-04T05:24:00.000000000Z
                        RXGZ\tnull\t1970-01-04T05:24:00.000000000Z
                        \tnull\t1970-01-04T05:24:00.000000000Z
                        \t1\t1970-01-04T08:24:00.000000000Z
                        CPSW\tnull\t1970-01-04T08:24:00.000000000Z
                        PEHN\tnull\t1970-01-04T08:24:00.000000000Z
                        HYRX\tnull\t1970-01-04T08:24:00.000000000Z
                        VTJW\tnull\t1970-01-04T08:24:00.000000000Z
                        RXGZ\tnull\t1970-01-04T08:24:00.000000000Z
                        WGRM\tnull\t1970-01-04T08:24:00.000000000Z
                        NPIW\tnull\t1970-01-04T08:24:00.000000000Z
                        CGFN\tnull\t1970-01-04T08:24:00.000000000Z
                        ZNFK\tnull\t1970-01-04T08:24:00.000000000Z
                        PEVM\tnull\t1970-01-04T08:24:00.000000000Z
                        """,
                true,
                true,
                false
        );

        execute("drop table x");

        assertQuery(
                """
                        b\tcount\tk
                        PVKN\t1\t1970-01-03T06:00:00.000000000Z
                        WQXY\t1\t1970-01-03T06:00:00.000000000Z
                        NLRH\tnull\t1970-01-03T06:00:00.000000000Z
                        CBWL\tnull\t1970-01-03T06:00:00.000000000Z
                        PFYX\tnull\t1970-01-03T06:00:00.000000000Z
                        \tnull\t1970-01-03T06:00:00.000000000Z
                        NLRH\t1\t1970-01-03T09:00:00.000000000Z
                        CBWL\t1\t1970-01-03T09:00:00.000000000Z
                        PFYX\t1\t1970-01-03T09:00:00.000000000Z
                        \t1\t1970-01-03T09:00:00.000000000Z
                        PVKN\tnull\t1970-01-03T09:00:00.000000000Z
                        WQXY\tnull\t1970-01-03T09:00:00.000000000Z
                        """,
                "select b, count(), k from (x latest on k partition by b) sample by 3h fill(linear) align to calendar",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 360000000000) k" +
                        " from" +
                        " long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 360000000) k" +
                        " from" +
                        " long_sequence(35)" +
                        ") timestamp(k)",
                """
                        b\tcount\tk
                        PVKN\t1\t1970-01-03T06:00:00.000000000Z
                        WQXY\t1\t1970-01-03T06:00:00.000000000Z
                        NLRH\tnull\t1970-01-03T06:00:00.000000000Z
                        CBWL\tnull\t1970-01-03T06:00:00.000000000Z
                        PFYX\tnull\t1970-01-03T06:00:00.000000000Z
                        QWPK\tnull\t1970-01-03T06:00:00.000000000Z
                        ONNL\tnull\t1970-01-03T06:00:00.000000000Z
                        LHTI\tnull\t1970-01-03T06:00:00.000000000Z
                        LFCY\tnull\t1970-01-03T06:00:00.000000000Z
                        \tnull\t1970-01-03T06:00:00.000000000Z
                        ITWG\tnull\t1970-01-03T06:00:00.000000000Z
                        NLRH\t1\t1970-01-03T09:00:00.000000000Z
                        CBWL\t1\t1970-01-03T09:00:00.000000000Z
                        PFYX\t1\t1970-01-03T09:00:00.000000000Z
                        PVKN\tnull\t1970-01-03T09:00:00.000000000Z
                        WQXY\tnull\t1970-01-03T09:00:00.000000000Z
                        QWPK\tnull\t1970-01-03T09:00:00.000000000Z
                        ONNL\tnull\t1970-01-03T09:00:00.000000000Z
                        LHTI\tnull\t1970-01-03T09:00:00.000000000Z
                        LFCY\tnull\t1970-01-03T09:00:00.000000000Z
                        \tnull\t1970-01-03T09:00:00.000000000Z
                        ITWG\tnull\t1970-01-03T09:00:00.000000000Z
                        PVKN\tnull\t1970-01-03T12:00:00.000000000Z
                        WQXY\tnull\t1970-01-03T12:00:00.000000000Z
                        NLRH\tnull\t1970-01-03T12:00:00.000000000Z
                        CBWL\tnull\t1970-01-03T12:00:00.000000000Z
                        PFYX\tnull\t1970-01-03T12:00:00.000000000Z
                        QWPK\tnull\t1970-01-03T12:00:00.000000000Z
                        ONNL\tnull\t1970-01-03T12:00:00.000000000Z
                        LHTI\tnull\t1970-01-03T12:00:00.000000000Z
                        LFCY\tnull\t1970-01-03T12:00:00.000000000Z
                        \tnull\t1970-01-03T12:00:00.000000000Z
                        ITWG\tnull\t1970-01-03T12:00:00.000000000Z
                        PVKN\tnull\t1970-01-03T15:00:00.000000000Z
                        WQXY\tnull\t1970-01-03T15:00:00.000000000Z
                        NLRH\tnull\t1970-01-03T15:00:00.000000000Z
                        CBWL\tnull\t1970-01-03T15:00:00.000000000Z
                        PFYX\tnull\t1970-01-03T15:00:00.000000000Z
                        QWPK\tnull\t1970-01-03T15:00:00.000000000Z
                        ONNL\tnull\t1970-01-03T15:00:00.000000000Z
                        LHTI\tnull\t1970-01-03T15:00:00.000000000Z
                        LFCY\tnull\t1970-01-03T15:00:00.000000000Z
                        \tnull\t1970-01-03T15:00:00.000000000Z
                        ITWG\tnull\t1970-01-03T15:00:00.000000000Z
                        PVKN\tnull\t1970-01-03T18:00:00.000000000Z
                        WQXY\tnull\t1970-01-03T18:00:00.000000000Z
                        NLRH\tnull\t1970-01-03T18:00:00.000000000Z
                        CBWL\tnull\t1970-01-03T18:00:00.000000000Z
                        PFYX\tnull\t1970-01-03T18:00:00.000000000Z
                        QWPK\tnull\t1970-01-03T18:00:00.000000000Z
                        ONNL\tnull\t1970-01-03T18:00:00.000000000Z
                        LHTI\tnull\t1970-01-03T18:00:00.000000000Z
                        LFCY\tnull\t1970-01-03T18:00:00.000000000Z
                        \tnull\t1970-01-03T18:00:00.000000000Z
                        ITWG\tnull\t1970-01-03T18:00:00.000000000Z
                        PVKN\tnull\t1970-01-03T21:00:00.000000000Z
                        WQXY\tnull\t1970-01-03T21:00:00.000000000Z
                        NLRH\tnull\t1970-01-03T21:00:00.000000000Z
                        CBWL\tnull\t1970-01-03T21:00:00.000000000Z
                        PFYX\tnull\t1970-01-03T21:00:00.000000000Z
                        QWPK\tnull\t1970-01-03T21:00:00.000000000Z
                        ONNL\tnull\t1970-01-03T21:00:00.000000000Z
                        LHTI\tnull\t1970-01-03T21:00:00.000000000Z
                        LFCY\tnull\t1970-01-03T21:00:00.000000000Z
                        \tnull\t1970-01-03T21:00:00.000000000Z
                        ITWG\tnull\t1970-01-03T21:00:00.000000000Z
                        PVKN\tnull\t1970-01-04T00:00:00.000000000Z
                        WQXY\tnull\t1970-01-04T00:00:00.000000000Z
                        NLRH\tnull\t1970-01-04T00:00:00.000000000Z
                        CBWL\tnull\t1970-01-04T00:00:00.000000000Z
                        PFYX\tnull\t1970-01-04T00:00:00.000000000Z
                        QWPK\tnull\t1970-01-04T00:00:00.000000000Z
                        ONNL\tnull\t1970-01-04T00:00:00.000000000Z
                        LHTI\tnull\t1970-01-04T00:00:00.000000000Z
                        LFCY\tnull\t1970-01-04T00:00:00.000000000Z
                        \tnull\t1970-01-04T00:00:00.000000000Z
                        ITWG\tnull\t1970-01-04T00:00:00.000000000Z
                        QWPK\t1\t1970-01-04T03:00:00.000000000Z
                        PVKN\tnull\t1970-01-04T03:00:00.000000000Z
                        WQXY\tnull\t1970-01-04T03:00:00.000000000Z
                        NLRH\tnull\t1970-01-04T03:00:00.000000000Z
                        CBWL\tnull\t1970-01-04T03:00:00.000000000Z
                        PFYX\tnull\t1970-01-04T03:00:00.000000000Z
                        ONNL\tnull\t1970-01-04T03:00:00.000000000Z
                        LHTI\tnull\t1970-01-04T03:00:00.000000000Z
                        LFCY\tnull\t1970-01-04T03:00:00.000000000Z
                        \tnull\t1970-01-04T03:00:00.000000000Z
                        ITWG\tnull\t1970-01-04T03:00:00.000000000Z
                        ONNL\t1\t1970-01-04T06:00:00.000000000Z
                        LHTI\t1\t1970-01-04T06:00:00.000000000Z
                        LFCY\t1\t1970-01-04T06:00:00.000000000Z
                        \t1\t1970-01-04T06:00:00.000000000Z
                        ITWG\t1\t1970-01-04T06:00:00.000000000Z
                        PVKN\tnull\t1970-01-04T06:00:00.000000000Z
                        WQXY\tnull\t1970-01-04T06:00:00.000000000Z
                        NLRH\tnull\t1970-01-04T06:00:00.000000000Z
                        CBWL\tnull\t1970-01-04T06:00:00.000000000Z
                        PFYX\tnull\t1970-01-04T06:00:00.000000000Z
                        QWPK\tnull\t1970-01-04T06:00:00.000000000Z
                        """,
                true,
                true,
                false
        );
    }

    @Test
    public void testSampleCountFillLinearWithOffset() throws Exception {
        assertQuery("""
                b\tcount\tk
                \t9\t1970-01-03T00:45:00.000000000Z
                VTJW\t2\t1970-01-03T00:45:00.000000000Z
                RXGZ\t1\t1970-01-03T00:45:00.000000000Z
                PEHN\t4\t1970-01-03T00:45:00.000000000Z
                HYRX\t2\t1970-01-03T00:45:00.000000000Z
                CPSW\t5\t1970-01-03T00:45:00.000000000Z
                \t15\t1970-01-03T03:45:00.000000000Z
                RXGZ\t2\t1970-01-03T03:45:00.000000000Z
                PEHN\t1\t1970-01-03T03:45:00.000000000Z
                VTJW\t5\t1970-01-03T03:45:00.000000000Z
                CPSW\t4\t1970-01-03T03:45:00.000000000Z
                HYRX\t3\t1970-01-03T03:45:00.000000000Z
                \t15\t1970-01-03T06:45:00.000000000Z
                CPSW\t3\t1970-01-03T06:45:00.000000000Z
                PEHN\t2\t1970-01-03T06:45:00.000000000Z
                HYRX\t4\t1970-01-03T06:45:00.000000000Z
                RXGZ\t3\t1970-01-03T06:45:00.000000000Z
                VTJW\t3\t1970-01-03T06:45:00.000000000Z
                \t11\t1970-01-03T09:45:00.000000000Z
                RXGZ\t5\t1970-01-03T09:45:00.000000000Z
                PEHN\t1\t1970-01-03T09:45:00.000000000Z
                HYRX\t1\t1970-01-03T09:45:00.000000000Z
                VTJW\t4\t1970-01-03T09:45:00.000000000Z
                CPSW\t2\t1970-01-03T09:45:00.000000000Z
                """, "select b, count(), k from x sample by 3h fill(linear) align to calendar time zone 'Europe/Berlin'with offset '00:45'", "create table x as " +
                "(" +
                "select" +
                " rnd_double(0)*100 a," +
                " rnd_symbol(5,4,4,1) b," +
                " timestamp_sequence_ns(cast('1970-01-03T02:00:00.000000000Z' as timestamp_ns), 360000000000) k" +
                " from" +
                " long_sequence(100)" +
                ") timestamp(k) partition by NONE", "k", "insert into x select * from (" +
                "select" +
                " rnd_double(0)*100 a," +
                " rnd_symbol(5,4,4,1) b," +
                " timestamp_sequence_ns(cast('1970-01-03T13:10:00.000000000Z' as timestamp_ns), 360000000000) k" +
                " from" +
                " long_sequence(35)" +
                ") timestamp(k)", """
                b\tcount\tk
                \t9\t1970-01-03T00:45:00.000000000Z
                VTJW\t2\t1970-01-03T00:45:00.000000000Z
                RXGZ\t1\t1970-01-03T00:45:00.000000000Z
                PEHN\t4\t1970-01-03T00:45:00.000000000Z
                HYRX\t2\t1970-01-03T00:45:00.000000000Z
                CPSW\t5\t1970-01-03T00:45:00.000000000Z
                CGFN\tnull\t1970-01-03T00:45:00.000000000Z
                NPIW\tnull\t1970-01-03T00:45:00.000000000Z
                PEVM\t6\t1970-01-03T00:45:00.000000000Z
                WGRM\tnull\t1970-01-03T00:45:00.000000000Z
                ZNFK\t6\t1970-01-03T00:45:00.000000000Z
                \t15\t1970-01-03T03:45:00.000000000Z
                RXGZ\t2\t1970-01-03T03:45:00.000000000Z
                PEHN\t1\t1970-01-03T03:45:00.000000000Z
                VTJW\t5\t1970-01-03T03:45:00.000000000Z
                CPSW\t4\t1970-01-03T03:45:00.000000000Z
                HYRX\t3\t1970-01-03T03:45:00.000000000Z
                CGFN\tnull\t1970-01-03T03:45:00.000000000Z
                NPIW\tnull\t1970-01-03T03:45:00.000000000Z
                PEVM\t5\t1970-01-03T03:45:00.000000000Z
                WGRM\tnull\t1970-01-03T03:45:00.000000000Z
                ZNFK\t5\t1970-01-03T03:45:00.000000000Z
                \t15\t1970-01-03T06:45:00.000000000Z
                CPSW\t3\t1970-01-03T06:45:00.000000000Z
                PEHN\t2\t1970-01-03T06:45:00.000000000Z
                HYRX\t4\t1970-01-03T06:45:00.000000000Z
                RXGZ\t3\t1970-01-03T06:45:00.000000000Z
                VTJW\t3\t1970-01-03T06:45:00.000000000Z
                CGFN\tnull\t1970-01-03T06:45:00.000000000Z
                NPIW\tnull\t1970-01-03T06:45:00.000000000Z
                PEVM\t4\t1970-01-03T06:45:00.000000000Z
                WGRM\tnull\t1970-01-03T06:45:00.000000000Z
                ZNFK\t4\t1970-01-03T06:45:00.000000000Z
                \t11\t1970-01-03T09:45:00.000000000Z
                RXGZ\t5\t1970-01-03T09:45:00.000000000Z
                PEHN\t1\t1970-01-03T09:45:00.000000000Z
                HYRX\t1\t1970-01-03T09:45:00.000000000Z
                VTJW\t4\t1970-01-03T09:45:00.000000000Z
                CPSW\t2\t1970-01-03T09:45:00.000000000Z
                CGFN\tnull\t1970-01-03T09:45:00.000000000Z
                NPIW\tnull\t1970-01-03T09:45:00.000000000Z
                PEVM\t3\t1970-01-03T09:45:00.000000000Z
                WGRM\tnull\t1970-01-03T09:45:00.000000000Z
                ZNFK\t3\t1970-01-03T09:45:00.000000000Z
                \t14\t1970-01-03T12:45:00.000000000Z
                CGFN\t3\t1970-01-03T12:45:00.000000000Z
                NPIW\t2\t1970-01-03T12:45:00.000000000Z
                PEVM\t2\t1970-01-03T12:45:00.000000000Z
                WGRM\t3\t1970-01-03T12:45:00.000000000Z
                ZNFK\t2\t1970-01-03T12:45:00.000000000Z
                VTJW\t5\t1970-01-03T12:45:00.000000000Z
                RXGZ\t7\t1970-01-03T12:45:00.000000000Z
                PEHN\t0\t1970-01-03T12:45:00.000000000Z
                HYRX\t-2\t1970-01-03T12:45:00.000000000Z
                CPSW\t1\t1970-01-03T12:45:00.000000000Z
                \t7\t1970-01-03T15:45:00.000000000Z
                ZNFK\t1\t1970-01-03T15:45:00.000000000Z
                PEVM\t1\t1970-01-03T15:45:00.000000000Z
                VTJW\t6\t1970-01-03T15:45:00.000000000Z
                RXGZ\t9\t1970-01-03T15:45:00.000000000Z
                PEHN\t-1\t1970-01-03T15:45:00.000000000Z
                HYRX\t-5\t1970-01-03T15:45:00.000000000Z
                CPSW\t0\t1970-01-03T15:45:00.000000000Z
                CGFN\tnull\t1970-01-03T15:45:00.000000000Z
                NPIW\tnull\t1970-01-03T15:45:00.000000000Z
                WGRM\tnull\t1970-01-03T15:45:00.000000000Z
                """, true, true, false);
    }

    @Test
    public void testSampleFillAllTypesLinearNoData() throws Exception {
        // sum_t tests memory leak
        assertQuery("b\tsum_t\tsum\tsum1\tsum2\tsum3\tsum4\tk\n",
                "select b, sum_t(a), sum(c), sum(d), sum(e), sum(f), sum(g), k from x sample by 3h fill(linear) align to first observation", "create table x as " +
                        "(" +
                        "select" +
                        " rnd_float(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " rnd_double(0)*100 c," +
                        " abs(rnd_int()) d," +
                        " rnd_byte(2, 50) e," +
                        " abs(rnd_short()) f," +
                        " abs(rnd_long()) g," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(0)" +
                        ") timestamp(k) partition by NONE", "k", "insert into x select * from (" +
                        "select" +
                        " rnd_float(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " rnd_double(0)*100 c," +
                        " abs(rnd_int()) d," +
                        " rnd_byte(2, 50) e," +
                        " abs(rnd_short()) f," +
                        " abs(rnd_long()) g," +
                        " timestamp_sequence('1970-01-04T05:00:00.000000000Z', 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)", """
                        b\tsum_t\tsum\tsum1\tsum2\tsum3\tsum4\tk
                        \t25.168644428253174\t96.69784438858017\t1715501826\t97\t28323\t-3537127814486931722\t1970-01-04T05:00:00.000000000Z
                        DEYY\t96.87422943115234\t67.00476391801053\t44173540\t34\t3282\t6794405451419334859\t1970-01-04T05:00:00.000000000Z
                        SXUX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T05:00:00.000000000Z
                        SXUX\t26.922100067138672\t52.98405941762054\t936627841\t16\t5741\t7153335833712179123\t1970-01-04T08:00:00.000000000Z
                        DEYY\t29.313718795776367\t16.47436916993191\t66297136\t4\t3428\t9036423629723776443\t1970-01-04T08:00:00.000000000Z
                        \tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T08:00:00.000000000Z
                        """, true, true, false);

        execute("drop table x");

        assertQuery("b\tsum_t\tsum\tsum1\tsum2\tsum3\tsum4\tk\n",
                "select b, sum_t(a), sum(c), sum(d), sum(e), sum(f), sum(g), k from x sample by 3h fill(linear) align to calendar", "create table x as " +
                        "(" +
                        "select" +
                        " rnd_float(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " rnd_double(0)*100 c," +
                        " abs(rnd_int()) d," +
                        " rnd_byte(2, 50) e," +
                        " abs(rnd_short()) f," +
                        " abs(rnd_long()) g," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(0)" +
                        ") timestamp(k) partition by NONE", "k", "insert into x select * from (" +
                        "select" +
                        " rnd_float(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " rnd_double(0)*100 c," +
                        " abs(rnd_int()) d," +
                        " rnd_byte(2, 50) e," +
                        " abs(rnd_short()) f," +
                        " abs(rnd_long()) g," +
                        " timestamp_sequence('1970-01-04T05:00:00.000000000Z' , 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)", """
                        b\tsum_t\tsum\tsum1\tsum2\tsum3\tsum4\tk
                        UVSD\t76.92382049560547\t49.42890511958454\t2075675260\t27\t1756\t6190031864817509934\t1970-01-04T03:00:00.000000000Z
                        \t96.74316048622131\t93.12424109486786\t1636133449\t-32\t50923\t7035175691104559104\t1970-01-04T03:00:00.000000000Z
                        KGHV\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T03:00:00.000000000Z
                        \t49.71342086791992\t48.55868295807066\t876466531\t5\t29572\t5710210982977201267\t1970-01-04T06:00:00.000000000Z
                        KGHV\t34.947265625\t28.799739396819312\t2124174232\t11\t25974\t7103100524321179064\t1970-01-04T06:00:00.000000000Z
                        UVSD\t24.008358001708984\t94.55893004802432\t2111250190\t24\t5869\t7973684666911773753\t1970-01-04T06:00:00.000000000Z
                        \t2.6836812496185303\t3.993124821273464\t116799613\t42\t8221\t4385246274849842834\t1970-01-04T09:00:00.000000000Z
                        UVSD\t-28.907104492187493\t139.6889549764641\t2146825120\t21\t9982\t9223372036854775807\t1970-01-04T09:00:00.000000000Z
                        KGHV\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T09:00:00.000000000Z
                        """, true, true, false);
    }

    @Test
    public void testSampleFillLinear() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\t60.41913029841844\t1970-01-03T00:00:00.000000000Z
                        HYRX\t269.08084955586986\t1970-01-03T00:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        VTJW\t44.39196261932496\t1970-01-03T03:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T03:00:00.000000000Z
                        HYRX\t183.39594050819096\t1970-01-03T03:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        VTJW\t46.60623681895594\t1970-01-03T06:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T06:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T09:00:00.000000000Z
                        PEHN\t82.96033060855811\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z
                        VTJW\t51.03478521821792\t1970-01-03T12:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T12:00:00.000000000Z
                        HYRX\t-73.65878663484578\t1970-01-03T12:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z
                        VTJW\t53.249059417848926\t1970-01-03T15:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T15:00:00.000000000Z
                        HYRX\t-159.34369568252467\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        VTJW\t55.463333617479904\t1970-01-03T18:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T18:00:00.000000000Z
                        PEHN\t13.557627225594144\t1970-01-03T18:00:00.000000000Z
                        HYRX\t-245.02860473020363\t1970-01-03T18:00:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 3h fill(linear)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\t60.41913029841844\t1970-01-03T00:00:00.000000000Z
                        HYRX\t269.08084955586986\t1970-01-03T00:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T00:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T00:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        VTJW\t44.39196261932496\t1970-01-03T03:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T03:00:00.000000000Z
                        HYRX\t183.39594050819096\t1970-01-03T03:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T03:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T03:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        VTJW\t46.60623681895594\t1970-01-03T06:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T06:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T06:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T06:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T09:00:00.000000000Z
                        PEHN\t82.96033060855811\t1970-01-03T09:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T09:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z
                        VTJW\t51.03478521821792\t1970-01-03T12:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T12:00:00.000000000Z
                        HYRX\t-73.65878663484578\t1970-01-03T12:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T12:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T12:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z
                        VTJW\t53.249059417848926\t1970-01-03T15:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T15:00:00.000000000Z
                        HYRX\t-159.34369568252467\t1970-01-03T15:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T15:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        VTJW\t55.463333617479904\t1970-01-03T18:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T18:00:00.000000000Z
                        PEHN\t13.557627225594144\t1970-01-03T18:00:00.000000000Z
                        HYRX\t-245.02860473020363\t1970-01-03T18:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T18:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T18:00:00.000000000Z
                        \t75.55713454429454\t1970-01-03T21:00:00.000000000Z
                        VTJW\t57.6776078171109\t1970-01-03T21:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T21:00:00.000000000Z
                        PEHN\t-21.889850047664098\t1970-01-03T21:00:00.000000000Z
                        HYRX\t-330.7135137778825\t1970-01-03T21:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T21:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T21:00:00.000000000Z
                        \t65.02434237974201\t1970-01-04T00:00:00.000000000Z
                        VTJW\t59.891882016741896\t1970-01-04T00:00:00.000000000Z
                        RXGZ\tnull\t1970-01-04T00:00:00.000000000Z
                        PEHN\t-57.33732732092234\t1970-01-04T00:00:00.000000000Z
                        HYRX\t-416.3984228255614\t1970-01-04T00:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T00:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T00:00:00.000000000Z
                        \t54.49155021518948\t1970-01-04T03:00:00.000000000Z
                        VTJW\t62.106156216372874\t1970-01-04T03:00:00.000000000Z
                        RXGZ\tnull\t1970-01-04T03:00:00.000000000Z
                        PEHN\t-92.7848045941806\t1970-01-04T03:00:00.000000000Z
                        HYRX\t-502.08333187324035\t1970-01-04T03:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T03:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T03:00:00.000000000Z
                        \t135.835983782176\t1970-01-04T06:00:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T06:00:00.000000000Z
                        VTJW\t64.32043041600389\t1970-01-04T06:00:00.000000000Z
                        RXGZ\tnull\t1970-01-04T06:00:00.000000000Z
                        PEHN\t-128.2322818674388\t1970-01-04T06:00:00.000000000Z
                        HYRX\t-587.7682409209193\t1970-01-04T06:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T06:00:00.000000000Z
                        KGHV\t67.52509547112409\t1970-01-04T09:00:00.000000000Z
                        \t217.1804173491625\t1970-01-04T09:00:00.000000000Z
                        VTJW\t66.53470461563487\t1970-01-04T09:00:00.000000000Z
                        RXGZ\tnull\t1970-01-04T09:00:00.000000000Z
                        PEHN\t-163.6797591406971\t1970-01-04T09:00:00.000000000Z
                        HYRX\t-673.4531499685982\t1970-01-04T09:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T09:00:00.000000000Z
                        """,
                true,
                true,
                false
        );
    }

    @Test
    public void testSampleFillLinearBadType() throws Exception {
        assertException(
                "select b, sum_t(b), k from x sample by 3h fill(linear)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_str(1,1,2) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                10,
                "support for LINEAR fill is not yet implemented"
        );
    }

    @Test
    public void testSampleFillLinearByMonth() throws Exception {
        assertQuery(
                """
                        b\tsum_t\tk
                        \t54112.40405938657\t1970-01-03T00:00:00.000000000Z
                        VTJW\t11209.880434660998\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t9939.438287132381\t1970-01-03T00:00:00.000000000Z
                        PEHN\t11042.882403279875\t1970-01-03T00:00:00.000000000Z
                        HYRX\t11080.174817969955\t1970-01-03T00:00:00.000000000Z
                        CPSW\t9310.397369439\t1970-01-03T00:00:00.000000000Z
                        \t53936.039113863764\t1970-04-03T00:00:00.000000000Z
                        HYRX\t10382.092656987053\t1970-04-03T00:00:00.000000000Z
                        CPSW\t11677.451781387846\t1970-04-03T00:00:00.000000000Z
                        RXGZ\t12082.97398092452\t1970-04-03T00:00:00.000000000Z
                        VTJW\t11574.354700279142\t1970-04-03T00:00:00.000000000Z
                        PEHN\t11225.427167029598\t1970-04-03T00:00:00.000000000Z
                        \t53719.38559836983\t1970-07-03T00:00:00.000000000Z
                        VTJW\t10645.216313875992\t1970-07-03T00:00:00.000000000Z
                        RXGZ\t12441.881371617534\t1970-07-03T00:00:00.000000000Z
                        HYRX\t10478.918039106036\t1970-07-03T00:00:00.000000000Z
                        CPSW\t11215.534064219255\t1970-07-03T00:00:00.000000000Z
                        PEHN\t12053.625707887684\t1970-07-03T00:00:00.000000000Z
                        \t54106.362147164444\t1970-10-03T00:00:00.000000000Z
                        HYRX\t11883.354138407445\t1970-10-03T00:00:00.000000000Z
                        RXGZ\t11608.715762809448\t1970-10-03T00:00:00.000000000Z
                        CPSW\t11623.362686708584\t1970-10-03T00:00:00.000000000Z
                        PEHN\t11258.550294609915\t1970-10-03T00:00:00.000000000Z
                        VTJW\t10865.136275604094\t1970-10-03T00:00:00.000000000Z
                        \t33152.56289929654\t1971-01-03T00:00:00.000000000Z
                        PEHN\t7219.25966062438\t1971-01-03T00:00:00.000000000Z
                        CPSW\t6038.83487182006\t1971-01-03T00:00:00.000000000Z
                        RXGZ\t5862.505042201944\t1971-01-03T00:00:00.000000000Z
                        VTJW\t6677.581919995402\t1971-01-03T00:00:00.000000000Z
                        HYRX\t5998.730211949621\t1971-01-03T00:00:00.000000000Z
                        """,
                "select b, sum_t(a), k from x sample by 3M fill(linear) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(10000)" +
                        ") timestamp(k) partition by NONE",
                "k",
                true,
                true
        );

        assertQuery(
                """
                        b\tsum_t\tk
                        \t54112.40405938657\t1970-01-01T00:00:00.000000000Z
                        VTJW\t11209.880434660998\t1970-01-01T00:00:00.000000000Z
                        RXGZ\t9939.438287132381\t1970-01-01T00:00:00.000000000Z
                        PEHN\t11042.882403279875\t1970-01-01T00:00:00.000000000Z
                        HYRX\t11080.174817969955\t1970-01-01T00:00:00.000000000Z
                        CPSW\t9310.397369439\t1970-01-01T00:00:00.000000000Z
                        \t53936.039113863764\t1970-04-01T00:00:00.000000000Z
                        HYRX\t10382.092656987053\t1970-04-01T00:00:00.000000000Z
                        CPSW\t11677.451781387846\t1970-04-01T00:00:00.000000000Z
                        RXGZ\t12082.97398092452\t1970-04-01T00:00:00.000000000Z
                        VTJW\t11574.354700279142\t1970-04-01T00:00:00.000000000Z
                        PEHN\t11225.427167029598\t1970-04-01T00:00:00.000000000Z
                        \t53719.38559836983\t1970-07-01T00:00:00.000000000Z
                        VTJW\t10645.216313875992\t1970-07-01T00:00:00.000000000Z
                        RXGZ\t12441.881371617534\t1970-07-01T00:00:00.000000000Z
                        HYRX\t10478.918039106036\t1970-07-01T00:00:00.000000000Z
                        CPSW\t11215.534064219255\t1970-07-01T00:00:00.000000000Z
                        PEHN\t12053.625707887684\t1970-07-01T00:00:00.000000000Z
                        \t54106.362147164444\t1970-10-01T00:00:00.000000000Z
                        HYRX\t11883.354138407445\t1970-10-01T00:00:00.000000000Z
                        RXGZ\t11608.715762809448\t1970-10-01T00:00:00.000000000Z
                        CPSW\t11623.362686708584\t1970-10-01T00:00:00.000000000Z
                        PEHN\t11258.550294609915\t1970-10-01T00:00:00.000000000Z
                        VTJW\t10865.136275604094\t1970-10-01T00:00:00.000000000Z
                        \t33152.56289929654\t1971-01-01T00:00:00.000000000Z
                        PEHN\t7219.25966062438\t1971-01-01T00:00:00.000000000Z
                        CPSW\t6038.83487182006\t1971-01-01T00:00:00.000000000Z
                        RXGZ\t5862.505042201944\t1971-01-01T00:00:00.000000000Z
                        VTJW\t6677.581919995402\t1971-01-01T00:00:00.000000000Z
                        HYRX\t5998.730211949621\t1971-01-01T00:00:00.000000000Z
                        """,
                "select b, sum_t(a), k from x sample by 3M fill(linear) align to calendar",
                "k",
                true,
                true
        );
    }

    @Test
    public void testSampleFillLinearConstructorFail() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table x as " +
                    "(" +
                    "select" +
                    " rnd_double(0)*100 a," +
                    " rnd_symbol(5,4,4,1) b," +
                    " timestamp_sequence_ns(172800000000000, 3600000000) k" +
                    " from" +
                    " long_sequence(20000000)" +
                    ") timestamp(k) partition by NONE"
            );

            CairoConfiguration configuration = createMmapFailingConfiguration(5);

            try (CairoEngine engine = new CairoEngine(configuration)) {
                try (SqlCompiler compiler = engine.getSqlCompiler()) {
                    try (SqlExecutionContextImpl ctx = new SqlExecutionContextImpl(engine, 0)) {
                        compiler.compile("select b, sum(a), k from x sample by 3h fill(linear)", ctx);
                        Assert.fail();
                    } catch (SqlException e) {
                        Assert.assertTrue(Chars.contains(e.getMessage(), "could not mmap"));
                    }
                    Assert.assertEquals(0, engine.getBusyReaderCount());
                    Assert.assertEquals(0, engine.getBusyWriterCount());
                }
            }
        });
    }

    @Test
    public void testSampleFillLinearFail() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table x as " +
                    "(" +
                    "select" +
                    " rnd_double(0)*100 a," +
                    " rnd_symbol(5,4,4,1) b," +
                    " timestamp_sequence_ns(172800000000000, 3600000000) k" +
                    " from" +
                    " long_sequence(20000000)" +
                    ") timestamp(k) partition by NONE"
            );

            CairoConfiguration configuration = createMmapFailingConfiguration(10);

            try (CairoEngine engine = new CairoEngine(configuration)) {
                try (SqlCompiler compiler = engine.getSqlCompiler()) {
                    try {
                        try (
                                RecordCursorFactory factory = compiler.compile("select b, sum(a), k from x sample by 3h fill(linear)", sqlExecutionContext).getRecordCursorFactory();
                                RecordCursor cursor = factory.getCursor(new SqlExecutionContextStub(engine))
                        ) {
                            while (cursor.hasNext()) {
                            }
                        }
                        Assert.fail();
                    } catch (CairoException e) {
                        Assert.assertTrue(Chars.contains(e.getMessage(), "could not mmap"));
                    }
                    Assert.assertEquals(0, engine.getBusyReaderCount());
                    Assert.assertEquals(0, engine.getBusyWriterCount());
                }
            }
        });
    }

    @Test
    public void testSampleFillLinearWithAlignment() throws Exception {
        assertMemoryLeak(() -> {
            execute("""
                    create table x (
                      ts timestamp_ns
                    ) timestamp(ts) partition by day wal;""");

            execute("""
                    
                    insert into x (ts) values\s
                    ('2024-03-30T12:24:01.000000000Z'),
                    ('2024-03-30T12:24:31.000000000Z'),
                    ('2024-03-30T12:25:01.000000000Z'),
                    ('2024-03-30T12:25:31.000000000Z'),
                    ('2024-03-30T12:26:02.000000000Z'),
                    ('2024-03-30T12:26:32.000000000Z'),
                    ('2024-03-30T12:27:02.000000000Z'),
                    ('2024-03-30T12:27:31.000000000Z'),
                    ('2024-03-30T12:28:01.000000000Z'),
                    ('2024-03-30T12:28:32.000000000Z'),
                    ('2024-03-30T12:29:01.000000000Z'),
                    ('2024-03-30T12:29:32.000000000Z'),
                    ('2024-03-30T12:30:02.000000000Z'),
                    ('2024-03-30T12:30:31.000000000Z'),
                    ('2024-03-30T12:31:01.000000000Z'),
                    ('2024-03-30T12:31:32.000000000Z'),
                    ('2024-03-30T12:32:02.000000000Z'),
                    ('2024-03-30T12:32:32.000000000Z'),
                    ('2024-03-30T12:33:01.000000000Z'),
                    ('2024-03-30T12:33:31.000000000Z'),
                    ('2024-03-30T12:34:02.000000000Z'),
                    ('2024-03-30T12:34:31.000000000Z'),
                    ('2024-03-30T12:35:01.000000000Z'),
                    ('2024-03-30T12:35:32.000000000Z'),
                    ('2024-03-30T12:36:01.000000000Z'),
                    ('2024-03-30T12:36:32.000000000Z'),
                    ('2024-03-30T12:37:02.000000000Z'),
                    ('2024-03-30T12:37:32.000000000Z'),
                    ('2024-03-30T12:38:02.000000000Z'),
                    ('2024-03-30T12:38:31.000000000Z'),
                    ('2024-03-30T12:39:02.000000000Z'),
                    ('2024-03-30T12:39:32.000000000Z'),
                    ('2024-03-30T12:40:02.000000000Z'),
                    ('2024-03-30T12:40:31.000000000Z'),
                    ('2024-03-30T12:41:01.000000000Z'),
                    ('2024-03-30T12:56:32.000000000Z'),
                    ('2024-03-30T12:57:02.000000000Z'),
                    ('2024-03-30T13:02:29.000000000Z'),
                    ('2024-03-30T13:07:58.000000000Z'),
                    ('2024-03-30T17:07:12.000000000Z'),
                    ('2024-03-30T17:07:44.000000000Z'),
                    ('2024-03-30T17:08:28.000000000Z'),
                    ('2024-03-30T17:08:59.000000000Z'),
                    ('2024-03-30T17:09:28.000000000Z'),
                    ('2024-03-30T17:09:59.000000000Z'),
                    ('2024-03-30T17:10:29.000000000Z'),
                    ('2024-03-30T17:10:57.000000000Z'),
                    ('2024-03-30T17:11:28.000000000Z'),
                    ('2024-03-30T17:11:58.000000000Z'),
                    ('2024-03-30T17:12:28.000000000Z'),
                    ('2024-03-30T17:12:57.000000000Z'),
                    ('2024-03-30T17:13:29.000000000Z'),
                    ('2024-03-30T17:13:58.000000000Z'),
                    ('2024-03-30T17:14:27.000000000Z'),
                    ('2024-03-30T17:14:58.000000000Z'),
                    ('2024-03-30T17:15:29.000000000Z'),
                    ('2024-03-30T17:15:59.000000000Z'),
                    ('2024-03-30T17:16:28.000000000Z'),
                    ('2024-03-30T17:16:58.000000000Z')""");

            drainWalQueue();

            assertQueryNoLeakCheck(
                    """
                            ts\tcount
                            2024-03-30T12:24:01.000000000Z\t10
                            2024-03-30T12:29:01.000000000Z\t10
                            2024-03-30T12:34:01.000000000Z\t10
                            2024-03-30T12:39:01.000000000Z\t5
                            2024-03-30T12:54:01.000000000Z\t2
                            2024-03-30T12:59:01.000000000Z\t1
                            2024-03-30T13:04:01.000000000Z\t1
                            2024-03-30T17:04:01.000000000Z\t4
                            2024-03-30T17:09:01.000000000Z\t10
                            2024-03-30T17:14:01.000000000Z\t6
                            """,
                    "select ts, count() from x sample by 5m align to first observation\n",
                    "ts",
                    false,
                    false
            );

            assertQueryNoLeakCheck(
                    """
                            ts\tcount
                            2024-03-30T12:24:01.000000000Z\t10
                            2024-03-30T12:29:01.000000000Z\t10
                            2024-03-30T12:34:01.000000000Z\t10
                            2024-03-30T12:39:01.000000000Z\t5
                            2024-03-30T12:44:01.000000000Z\t4
                            2024-03-30T12:49:01.000000000Z\t3
                            2024-03-30T12:54:01.000000000Z\t2
                            2024-03-30T12:59:01.000000000Z\t1
                            2024-03-30T13:04:01.000000000Z\t1
                            2024-03-30T13:09:01.000000000Z\t1
                            2024-03-30T13:14:01.000000000Z\t1
                            2024-03-30T13:19:01.000000000Z\t1
                            2024-03-30T13:24:01.000000000Z\t1
                            2024-03-30T13:29:01.000000000Z\t1
                            2024-03-30T13:34:01.000000000Z\t1
                            2024-03-30T13:39:01.000000000Z\t1
                            2024-03-30T13:44:01.000000000Z\t1
                            2024-03-30T13:49:01.000000000Z\t1
                            2024-03-30T13:54:01.000000000Z\t1
                            2024-03-30T13:59:01.000000000Z\t1
                            2024-03-30T14:04:01.000000000Z\t1
                            2024-03-30T14:09:01.000000000Z\t1
                            2024-03-30T14:14:01.000000000Z\t1
                            2024-03-30T14:19:01.000000000Z\t1
                            2024-03-30T14:24:01.000000000Z\t2
                            2024-03-30T14:29:01.000000000Z\t2
                            2024-03-30T14:34:01.000000000Z\t2
                            2024-03-30T14:39:01.000000000Z\t2
                            2024-03-30T14:44:01.000000000Z\t2
                            2024-03-30T14:49:01.000000000Z\t2
                            2024-03-30T14:54:01.000000000Z\t2
                            2024-03-30T14:59:01.000000000Z\t2
                            2024-03-30T15:04:01.000000000Z\t2
                            2024-03-30T15:09:01.000000000Z\t2
                            2024-03-30T15:14:01.000000000Z\t2
                            2024-03-30T15:19:01.000000000Z\t2
                            2024-03-30T15:24:01.000000000Z\t2
                            2024-03-30T15:29:01.000000000Z\t2
                            2024-03-30T15:34:01.000000000Z\t2
                            2024-03-30T15:39:01.000000000Z\t2
                            2024-03-30T15:44:01.000000000Z\t3
                            2024-03-30T15:49:01.000000000Z\t3
                            2024-03-30T15:54:01.000000000Z\t3
                            2024-03-30T15:59:01.000000000Z\t3
                            2024-03-30T16:04:01.000000000Z\t3
                            2024-03-30T16:09:01.000000000Z\t3
                            2024-03-30T16:14:01.000000000Z\t3
                            2024-03-30T16:19:01.000000000Z\t3
                            2024-03-30T16:24:01.000000000Z\t3
                            2024-03-30T16:29:01.000000000Z\t3
                            2024-03-30T16:34:01.000000000Z\t3
                            2024-03-30T16:39:01.000000000Z\t3
                            2024-03-30T16:44:01.000000000Z\t3
                            2024-03-30T16:49:01.000000000Z\t3
                            2024-03-30T16:54:01.000000000Z\t3
                            2024-03-30T16:59:01.000000000Z\t3
                            2024-03-30T17:04:01.000000000Z\t4
                            2024-03-30T17:09:01.000000000Z\t10
                            2024-03-30T17:14:01.000000000Z\t6
                            """,
                    "select ts, count() from x sample by 5m fill(linear) align to first observation\n",
                    "ts",
                    true,
                    true
            );

            assertQueryNoLeakCheck(
                    """
                            ts\tcount
                            2024-03-30T12:20:00.000000000Z\t2
                            2024-03-30T12:25:00.000000000Z\t10
                            2024-03-30T12:30:00.000000000Z\t10
                            2024-03-30T12:35:00.000000000Z\t10
                            2024-03-30T12:40:00.000000000Z\t3
                            2024-03-30T12:55:00.000000000Z\t2
                            2024-03-30T13:00:00.000000000Z\t1
                            2024-03-30T13:05:00.000000000Z\t1
                            2024-03-30T17:05:00.000000000Z\t6
                            2024-03-30T17:10:00.000000000Z\t10
                            2024-03-30T17:15:00.000000000Z\t4
                            """,
                    "select ts, count() from x sample by 5m align to calendar\n",
                    "ts",
                    true,
                    true
            );

            assertQueryNoLeakCheck(
                    """
                            ts\tcount
                            2024-03-30T12:20:00.000000000Z\t2
                            2024-03-30T12:25:00.000000000Z\t10
                            2024-03-30T12:30:00.000000000Z\t10
                            2024-03-30T12:35:00.000000000Z\t10
                            2024-03-30T12:40:00.000000000Z\t3
                            2024-03-30T12:45:00.000000000Z\t2
                            2024-03-30T12:50:00.000000000Z\t2
                            2024-03-30T12:55:00.000000000Z\t2
                            2024-03-30T13:00:00.000000000Z\t1
                            2024-03-30T13:05:00.000000000Z\t1
                            2024-03-30T13:10:00.000000000Z\t1
                            2024-03-30T13:15:00.000000000Z\t1
                            2024-03-30T13:20:00.000000000Z\t1
                            2024-03-30T13:25:00.000000000Z\t1
                            2024-03-30T13:30:00.000000000Z\t1
                            2024-03-30T13:35:00.000000000Z\t1
                            2024-03-30T13:40:00.000000000Z\t1
                            2024-03-30T13:45:00.000000000Z\t1
                            2024-03-30T13:50:00.000000000Z\t1
                            2024-03-30T13:55:00.000000000Z\t2
                            2024-03-30T14:00:00.000000000Z\t2
                            2024-03-30T14:05:00.000000000Z\t2
                            2024-03-30T14:10:00.000000000Z\t2
                            2024-03-30T14:15:00.000000000Z\t2
                            2024-03-30T14:20:00.000000000Z\t2
                            2024-03-30T14:25:00.000000000Z\t2
                            2024-03-30T14:30:00.000000000Z\t2
                            2024-03-30T14:35:00.000000000Z\t2
                            2024-03-30T14:40:00.000000000Z\t2
                            2024-03-30T14:45:00.000000000Z\t3
                            2024-03-30T14:50:00.000000000Z\t3
                            2024-03-30T14:55:00.000000000Z\t3
                            2024-03-30T15:00:00.000000000Z\t3
                            2024-03-30T15:05:00.000000000Z\t3
                            2024-03-30T15:10:00.000000000Z\t3
                            2024-03-30T15:15:00.000000000Z\t3
                            2024-03-30T15:20:00.000000000Z\t3
                            2024-03-30T15:25:00.000000000Z\t3
                            2024-03-30T15:30:00.000000000Z\t4
                            2024-03-30T15:35:00.000000000Z\t4
                            2024-03-30T15:40:00.000000000Z\t4
                            2024-03-30T15:45:00.000000000Z\t4
                            2024-03-30T15:50:00.000000000Z\t4
                            2024-03-30T15:55:00.000000000Z\t4
                            2024-03-30T16:00:00.000000000Z\t4
                            2024-03-30T16:05:00.000000000Z\t4
                            2024-03-30T16:10:00.000000000Z\t4
                            2024-03-30T16:15:00.000000000Z\t4
                            2024-03-30T16:20:00.000000000Z\t5
                            2024-03-30T16:25:00.000000000Z\t5
                            2024-03-30T16:30:00.000000000Z\t5
                            2024-03-30T16:35:00.000000000Z\t5
                            2024-03-30T16:40:00.000000000Z\t5
                            2024-03-30T16:45:00.000000000Z\t5
                            2024-03-30T16:50:00.000000000Z\t5
                            2024-03-30T16:55:00.000000000Z\t5
                            2024-03-30T17:00:00.000000000Z\t5
                            2024-03-30T17:05:00.000000000Z\t6
                            2024-03-30T17:10:00.000000000Z\t10
                            2024-03-30T17:15:00.000000000Z\t4
                            """,
                    "select ts, count() from x sample by 5m fill(linear) align to calendar\n",
                    "ts",
                    true,
                    true
            );
        });
    }

    @Test
    public void testSampleFillNone() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 3h fill(none) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        \t54.49155021518948\t1970-01-04T03:00:00.000000000Z
                        \t135.835983782176\t1970-01-04T06:00:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T06:00:00.000000000Z
                        KGHV\t67.52509547112409\t1970-01-04T09:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillNoneAllTypes() throws Exception {
        assertQuery(
                """
                        b\tsum\tsum1\tsum2\tsum3\tsum4\tsum5\tk
                        \t74.19752505948932\t113.12129\t2557447177\t868\t12\t-6307312481136788016\t1970-01-03T00:00:00.000000000Z
                        CPSW\t0.35983672154330515\t76.75673\t113506296\t27809\t9\t-8889930662239044040\t1970-01-03T00:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T00:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T00:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T00:00:00.000000000Z
                        \t76.64256753596138\t55.224937\t326010667\t-5741\t8\t7392877322819819290\t1970-01-03T03:00:00.000000000Z
                        CPSW\t13.450170570900255\t34.35685\t410717394\t18229\t10\t6820495939660535106\t1970-01-03T03:00:00.000000000Z
                        PEHN\t15.786635599554755\t12.50304\t264240638\t-7976\t6\t-8480005421611953360\t1970-01-03T03:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T03:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T03:00:00.000000000Z
                        \t85.05940141744613\t92.16078\t301655269\t-14676\t12\t-2937111954994403426\t1970-01-03T06:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T06:00:00.000000000Z
                        PEHN\t86.64158914718531\t88.374214\t1566901076\t-3017\t3\t-5028301966399563827\t1970-01-03T06:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T06:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T06:00:00.000000000Z
                        \t106.78118249687527\t103.1198\t3029605432\t-2372\t12\t-1162868573414266742\t1970-01-03T09:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T09:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t3.831785863680992\t42.02044\t1254404167\t1756\t5\t8702525427024484485\t1970-01-03T09:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T09:00:00.000000000Z
                        \t117.60937843256664\t189.81728\t3717804370\t-27064\t17\t2215137494070785317\t1970-01-03T12:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T12:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T12:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T12:00:00.000000000Z
                        HYRX\t24.008362859107102\t76.57837\t2111250190\t-13252\t8\t7973684666911773753\t1970-01-03T12:00:00.000000000Z
                        \t28.087836621126815\t139.30695\t2587989045\t11751\t17\t-8594661640328306402\t1970-01-03T15:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T15:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T15:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T15:00:00.000000000Z
                        HYRX\t2.6836863013701473\t10.643042\t502711083\t-8221\t9\t-7709579215942154242\t1970-01-03T15:00:00.000000000Z
                        \t75.17160551750754\t120.51888\t2362241402\t514\t11\t-2863260545700031392\t1970-01-03T18:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T18:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T18:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T18:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T18:00:00.000000000Z
                        """,
                "select b, sum(a), sum(c), sum(d), sum(e), sum(f), sum(g), k from x sample by 3h fill(null)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " rnd_float(0)*100 c," +
                        " abs(rnd_int()) d," +
                        " rnd_short() e," +
                        " rnd_byte(3,10) f," +
                        " rnd_long() g," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " rnd_float(0)*100 c," +
                        " abs(rnd_int()) d," +
                        " rnd_short() e," +
                        " rnd_byte(3,10) f," +
                        " rnd_long() g," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum\tsum1\tsum2\tsum3\tsum4\tsum5\tk
                        \t74.19752505948932\t113.12129\t2557447177\t868\t12\t-6307312481136788016\t1970-01-03T00:00:00.000000000Z
                        CPSW\t0.35983672154330515\t76.75673\t113506296\t27809\t9\t-8889930662239044040\t1970-01-03T00:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T00:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T00:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T00:00:00.000000000Z
                        EZGH\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T00:00:00.000000000Z
                        FLOP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T00:00:00.000000000Z
                        WVDK\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T00:00:00.000000000Z
                        JOXP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T00:00:00.000000000Z
                        \t76.64256753596138\t55.224937\t326010667\t-5741\t8\t7392877322819819290\t1970-01-03T03:00:00.000000000Z
                        CPSW\t13.450170570900255\t34.35685\t410717394\t18229\t10\t6820495939660535106\t1970-01-03T03:00:00.000000000Z
                        PEHN\t15.786635599554755\t12.50304\t264240638\t-7976\t6\t-8480005421611953360\t1970-01-03T03:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T03:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T03:00:00.000000000Z
                        EZGH\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T03:00:00.000000000Z
                        FLOP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T03:00:00.000000000Z
                        WVDK\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T03:00:00.000000000Z
                        JOXP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T03:00:00.000000000Z
                        \t85.05940141744613\t92.16078\t301655269\t-14676\t12\t-2937111954994403426\t1970-01-03T06:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T06:00:00.000000000Z
                        PEHN\t86.64158914718531\t88.374214\t1566901076\t-3017\t3\t-5028301966399563827\t1970-01-03T06:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T06:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T06:00:00.000000000Z
                        EZGH\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T06:00:00.000000000Z
                        FLOP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T06:00:00.000000000Z
                        WVDK\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T06:00:00.000000000Z
                        JOXP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T06:00:00.000000000Z
                        \t106.78118249687527\t103.1198\t3029605432\t-2372\t12\t-1162868573414266742\t1970-01-03T09:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T09:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t3.831785863680992\t42.02044\t1254404167\t1756\t5\t8702525427024484485\t1970-01-03T09:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T09:00:00.000000000Z
                        EZGH\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T09:00:00.000000000Z
                        FLOP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T09:00:00.000000000Z
                        WVDK\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T09:00:00.000000000Z
                        JOXP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T09:00:00.000000000Z
                        \t117.60937843256664\t189.81728\t3717804370\t-27064\t17\t2215137494070785317\t1970-01-03T12:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T12:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T12:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T12:00:00.000000000Z
                        HYRX\t24.008362859107102\t76.57837\t2111250190\t-13252\t8\t7973684666911773753\t1970-01-03T12:00:00.000000000Z
                        EZGH\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T12:00:00.000000000Z
                        FLOP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T12:00:00.000000000Z
                        WVDK\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T12:00:00.000000000Z
                        JOXP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T12:00:00.000000000Z
                        \t28.087836621126815\t139.30695\t2587989045\t11751\t17\t-8594661640328306402\t1970-01-03T15:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T15:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T15:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T15:00:00.000000000Z
                        HYRX\t2.6836863013701473\t10.643042\t502711083\t-8221\t9\t-7709579215942154242\t1970-01-03T15:00:00.000000000Z
                        EZGH\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T15:00:00.000000000Z
                        FLOP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T15:00:00.000000000Z
                        WVDK\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T15:00:00.000000000Z
                        JOXP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T15:00:00.000000000Z
                        \t75.17160551750754\t120.51888\t2362241402\t514\t11\t-2863260545700031392\t1970-01-03T18:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T18:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T18:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T18:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T18:00:00.000000000Z
                        EZGH\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T18:00:00.000000000Z
                        FLOP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T18:00:00.000000000Z
                        WVDK\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T18:00:00.000000000Z
                        JOXP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T18:00:00.000000000Z
                        \tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T21:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T21:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T21:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T21:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T21:00:00.000000000Z
                        EZGH\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T21:00:00.000000000Z
                        FLOP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T21:00:00.000000000Z
                        WVDK\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T21:00:00.000000000Z
                        JOXP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-03T21:00:00.000000000Z
                        \tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T00:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T00:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T00:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T00:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T00:00:00.000000000Z
                        EZGH\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T00:00:00.000000000Z
                        FLOP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T00:00:00.000000000Z
                        WVDK\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T00:00:00.000000000Z
                        JOXP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T00:00:00.000000000Z
                        \t20.585069039325443\t98.8401\t1278547815\t17250\t3\t-6703401424236463520\t1970-01-04T03:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T03:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T03:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T03:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T03:00:00.000000000Z
                        EZGH\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T03:00:00.000000000Z
                        FLOP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T03:00:00.000000000Z
                        WVDK\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T03:00:00.000000000Z
                        JOXP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T03:00:00.000000000Z
                        \tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T06:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T06:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T06:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T06:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T06:00:00.000000000Z
                        EZGH\t5.0246156790690115\t38.422543\t370796356\t5422\t3\t4959459375462458218\t1970-01-04T06:00:00.000000000Z
                        FLOP\t17.180291960857296\t5.158454\t532016913\t-3028\t7\t2282781332678491916\t1970-01-04T06:00:00.000000000Z
                        WVDK\t54.66900921405317\t35.681107\t874367915\t-23001\t10\t9089874911309539983\t1970-01-04T06:00:00.000000000Z
                        JOXP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T06:00:00.000000000Z
                        \tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T09:00:00.000000000Z
                        CPSW\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T09:00:00.000000000Z
                        PEHN\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T09:00:00.000000000Z
                        RXGZ\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T09:00:00.000000000Z
                        HYRX\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T09:00:00.000000000Z
                        EZGH\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T09:00:00.000000000Z
                        FLOP\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T09:00:00.000000000Z
                        WVDK\tnull\tnull\tnull\tnull\tnull\tnull\t1970-01-04T09:00:00.000000000Z
                        JOXP\t67.29405590773638\t76.06252\t1165635863\t2316\t9\t-4547802916868961458\t1970-01-04T09:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillNoneDataGaps() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T01:20:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T01:50:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T02:50:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:50:00.000000000Z
                        \t87.99634725391621\t1970-01-03T04:20:00.000000000Z
                        \t32.881769076795045\t1970-01-03T05:20:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:20:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T07:20:00.000000000Z
                        \t57.93466326862211\t1970-01-03T07:50:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T08:50:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:50:00.000000000Z
                        \t26.922103479744898\t1970-01-03T10:20:00.000000000Z
                        \t52.98405941762054\t1970-01-03T11:20:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:20:00.000000000Z
                        \t97.5019885372507\t1970-01-03T13:20:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T13:50:00.000000000Z
                        \t80.01121139739173\t1970-01-03T14:50:00.000000000Z
                        \t92.050039469858\t1970-01-03T15:50:00.000000000Z
                        \t45.6344569609078\t1970-01-03T16:50:00.000000000Z
                        \t40.455469747939254\t1970-01-03T17:20:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 30m fill(none) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns('1970-01-03T01:20:00.000000000Z', 3100000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence('1970-01-04T05:00:00.000000000Z', 3200000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T01:20:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T01:50:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T02:50:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:50:00.000000000Z
                        \t87.99634725391621\t1970-01-03T04:20:00.000000000Z
                        \t32.881769076795045\t1970-01-03T05:20:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:20:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T07:20:00.000000000Z
                        \t57.93466326862211\t1970-01-03T07:50:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T08:50:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:50:00.000000000Z
                        \t26.922103479744898\t1970-01-03T10:20:00.000000000Z
                        \t52.98405941762054\t1970-01-03T11:20:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:20:00.000000000Z
                        \t97.5019885372507\t1970-01-03T13:20:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T13:50:00.000000000Z
                        \t80.01121139739173\t1970-01-03T14:50:00.000000000Z
                        \t92.050039469858\t1970-01-03T15:50:00.000000000Z
                        \t45.6344569609078\t1970-01-03T16:50:00.000000000Z
                        \t40.455469747939254\t1970-01-03T17:20:00.000000000Z
                        \t54.49155021518948\t1970-01-04T04:50:00.000000000Z
                        \t76.9238189433781\t1970-01-04T05:50:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T06:20:00.000000000Z
                        \t58.912164838797885\t1970-01-04T07:20:00.000000000Z
                        KGHV\t67.52509547112409\t1970-01-04T08:20:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillNoneDataGapsAlignToCalendar() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T01:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T02:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:30:00.000000000Z
                        \t87.99634725391621\t1970-01-03T04:30:00.000000000Z
                        \t32.881769076795045\t1970-01-03T05:30:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:30:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T07:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T08:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:30:00.000000000Z
                        \t26.922103479744898\t1970-01-03T10:30:00.000000000Z
                        \t52.98405941762054\t1970-01-03T11:30:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:30:00.000000000Z
                        \t97.5019885372507\t1970-01-03T13:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T14:00:00.000000000Z
                        \t80.01121139739173\t1970-01-03T15:00:00.000000000Z
                        \t92.050039469858\t1970-01-03T15:30:00.000000000Z
                        \t45.6344569609078\t1970-01-03T16:30:00.000000000Z
                        \t40.455469747939254\t1970-01-03T17:30:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 30m fill(none) align to calendar",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns('1970-01-03T01:20:00.000000000Z', 3100000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence('1970-01-04T05:00:00.000000000Z', 3200000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T01:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T02:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:30:00.000000000Z
                        \t87.99634725391621\t1970-01-03T04:30:00.000000000Z
                        \t32.881769076795045\t1970-01-03T05:30:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:30:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T07:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T08:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:30:00.000000000Z
                        \t26.922103479744898\t1970-01-03T10:30:00.000000000Z
                        \t52.98405941762054\t1970-01-03T11:30:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:30:00.000000000Z
                        \t97.5019885372507\t1970-01-03T13:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T14:00:00.000000000Z
                        \t80.01121139739173\t1970-01-03T15:00:00.000000000Z
                        \t92.050039469858\t1970-01-03T15:30:00.000000000Z
                        \t45.6344569609078\t1970-01-03T16:30:00.000000000Z
                        \t40.455469747939254\t1970-01-03T17:30:00.000000000Z
                        \t54.49155021518948\t1970-01-04T05:00:00.000000000Z
                        \t76.9238189433781\t1970-01-04T05:30:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T06:30:00.000000000Z
                        \t58.912164838797885\t1970-01-04T07:30:00.000000000Z
                        KGHV\t67.52509547112409\t1970-01-04T08:30:00.000000000Z
                        """,
                true,
                true,
                false
        );
    }

    @Test
    public void testSampleFillNoneDataGapsAlignToCalendarTimeZone() throws Exception {
        assertQuery(
                """
                        b\ts\tk
                        \t11.427984775756228\t2021-03-28T01:00:00.000000000Z
                        VTJW\t42.17768841969397\t2021-03-28T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-03-28T04:00:00.000000000Z
                        PEHN\t70.94360487171201\t2021-03-28T04:30:00.000000000Z
                        \t87.99634725391621\t2021-03-28T05:30:00.000000000Z
                        \t32.881769076795045\t2021-03-28T06:30:00.000000000Z
                        HYRX\t97.71103146051203\t2021-03-28T07:30:00.000000000Z
                        PEHN\t81.46807944500559\t2021-03-28T08:00:00.000000000Z
                        \t57.93466326862211\t2021-03-28T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t2021-03-28T10:00:00.000000000Z
                        VTJW\t48.820511018586934\t2021-03-28T10:30:00.000000000Z
                        \t26.922103479744898\t2021-03-28T11:30:00.000000000Z
                        \t52.98405941762054\t2021-03-28T12:30:00.000000000Z
                        PEHN\t84.45258177211063\t2021-03-28T13:30:00.000000000Z
                        \t97.5019885372507\t2021-03-28T14:00:00.000000000Z
                        PEHN\t49.00510449885239\t2021-03-28T15:00:00.000000000Z
                        \t80.01121139739173\t2021-03-28T16:00:00.000000000Z
                        \t92.050039469858\t2021-03-28T16:30:00.000000000Z
                        \t45.6344569609078\t2021-03-28T17:30:00.000000000Z
                        \t40.455469747939254\t2021-03-28T18:30:00.000000000Z
                        """,
                "select b, s, to_timezone(k, 'Europe/Madrid') k from (select b, sum(a) s, k from x sample by 30m fill(none) align to calendar time zone 'Europe/Madrid')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-03-28T00:20:00.000000000Z' as timestamp_ns), 3100000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                null,
                false,
                false
        );
    }

    @Test
    public void testSampleFillNoneEmpty() throws Exception {
        assertQuery(
                "b\tsum_t\tk\n",
                "select b, sum_t(a), k from x sample by 2h fill(none) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(0)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum_t\tk
                        IBBT\t0.35983672154330515\t1970-01-04T05:00:00.000000000Z
                        \t76.75673070796104\t1970-01-04T05:00:00.000000000Z
                        \t125.98934239031611\t1970-01-04T07:00:00.000000000Z
                        \t57.93466326862211\t1970-01-04T09:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillNoneNotKeyed() throws Exception {
        assertQuery(
                """
                        sum\tk
                        77.51096330391545\t1970-01-03T00:00:00.000000000Z
                        191.82172120242328\t1970-01-03T03:00:00.000000000Z
                        237.11377417413973\t1970-01-03T06:00:00.000000000Z
                        87.76873691116495\t1970-01-03T09:00:00.000000000Z
                        234.93862972698187\t1970-01-03T12:00:00.000000000Z
                        221.06635536610213\t1970-01-03T15:00:00.000000000Z
                        86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        """,
                "select sum(a), k from x sample by 3h fill(none) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        sum\tk
                        77.51096330391545\t1970-01-03T00:00:00.000000000Z
                        191.82172120242328\t1970-01-03T03:00:00.000000000Z
                        237.11377417413973\t1970-01-03T06:00:00.000000000Z
                        87.76873691116495\t1970-01-03T09:00:00.000000000Z
                        234.93862972698187\t1970-01-03T12:00:00.000000000Z
                        221.06635536610213\t1970-01-03T15:00:00.000000000Z
                        86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        54.49155021518948\t1970-01-04T03:00:00.000000000Z
                        185.26488890176051\t1970-01-04T06:00:00.000000000Z
                        67.52509547112409\t1970-01-04T09:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillNoneNotKeyedEmpty() throws Exception {
        assertQuery(
                "sum\tk\n",
                "select sum(a), k from x sample by 3h fill(none) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(0)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        sum\tk
                        139.2898345080353\t1970-01-04T05:00:00.000000000Z
                        121.75073858040724\t1970-01-04T08:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillNull() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\tnull\t1970-01-03T00:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T00:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        VTJW\tnull\t1970-01-03T03:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T03:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        VTJW\tnull\t1970-01-03T06:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T09:00:00.000000000Z
                        PEHN\tnull\t1970-01-03T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z
                        VTJW\tnull\t1970-01-03T12:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T12:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z
                        VTJW\tnull\t1970-01-03T15:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T15:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        VTJW\tnull\t1970-01-03T18:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T18:00:00.000000000Z
                        PEHN\tnull\t1970-01-03T18:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T18:00:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 3h fill(null)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\tnull\t1970-01-03T00:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T00:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T00:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T00:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        VTJW\tnull\t1970-01-03T03:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T03:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T03:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T03:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        VTJW\tnull\t1970-01-03T06:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T06:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T06:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T09:00:00.000000000Z
                        PEHN\tnull\t1970-01-03T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T09:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z
                        VTJW\tnull\t1970-01-03T12:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T12:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T12:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T12:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z
                        VTJW\tnull\t1970-01-03T15:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T15:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T15:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T15:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        VTJW\tnull\t1970-01-03T18:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T18:00:00.000000000Z
                        PEHN\tnull\t1970-01-03T18:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T18:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T18:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T18:00:00.000000000Z
                        \tnull\t1970-01-03T21:00:00.000000000Z
                        VTJW\tnull\t1970-01-03T21:00:00.000000000Z
                        RXGZ\tnull\t1970-01-03T21:00:00.000000000Z
                        PEHN\tnull\t1970-01-03T21:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T21:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T21:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T21:00:00.000000000Z
                        \tnull\t1970-01-04T00:00:00.000000000Z
                        VTJW\tnull\t1970-01-04T00:00:00.000000000Z
                        RXGZ\tnull\t1970-01-04T00:00:00.000000000Z
                        PEHN\tnull\t1970-01-04T00:00:00.000000000Z
                        HYRX\tnull\t1970-01-04T00:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T00:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T00:00:00.000000000Z
                        \t54.49155021518948\t1970-01-04T03:00:00.000000000Z
                        VTJW\tnull\t1970-01-04T03:00:00.000000000Z
                        RXGZ\tnull\t1970-01-04T03:00:00.000000000Z
                        PEHN\tnull\t1970-01-04T03:00:00.000000000Z
                        HYRX\tnull\t1970-01-04T03:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T03:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T03:00:00.000000000Z
                        \t135.835983782176\t1970-01-04T06:00:00.000000000Z
                        VTJW\tnull\t1970-01-04T06:00:00.000000000Z
                        RXGZ\tnull\t1970-01-04T06:00:00.000000000Z
                        PEHN\tnull\t1970-01-04T06:00:00.000000000Z
                        HYRX\tnull\t1970-01-04T06:00:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T06:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T06:00:00.000000000Z
                        \tnull\t1970-01-04T09:00:00.000000000Z
                        VTJW\tnull\t1970-01-04T09:00:00.000000000Z
                        RXGZ\tnull\t1970-01-04T09:00:00.000000000Z
                        PEHN\tnull\t1970-01-04T09:00:00.000000000Z
                        HYRX\tnull\t1970-01-04T09:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T09:00:00.000000000Z
                        KGHV\t67.52509547112409\t1970-01-04T09:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillNullAlignToCalendarTimeZone() throws Exception {
        // EST timezone has clock go back on 7 Nov. From -4 UTC to -5 UTC
        // at 6am UTC EST time is 2am (DTS), when clock goes back 7am also becomes 2am UTC
        // hence 6am UTC is duplicate timestamp and is expected to be compounded
        assertQuery(
                """
                        b\ts\tk
                        \t11.427984775756228\t2021-11-06T18:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T18:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T18:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T18:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T18:00:00.000000000Z
                        \tnull\t2021-11-06T19:00:00.000000000Z
                        VTJW\t42.17768841969397\t2021-11-06T19:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-06T19:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T19:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T19:00:00.000000000Z
                        \tnull\t2021-11-06T20:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T20:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T20:00:00.000000000Z
                        PEHN\t70.94360487171201\t2021-11-06T20:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T20:00:00.000000000Z
                        \t87.99634725391621\t2021-11-06T21:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T21:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T21:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T21:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T21:00:00.000000000Z
                        \t32.881769076795045\t2021-11-06T22:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T22:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T22:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T22:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T22:00:00.000000000Z
                        \tnull\t2021-11-06T23:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T23:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T23:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T23:00:00.000000000Z
                        HYRX\t97.71103146051203\t2021-11-06T23:00:00.000000000Z
                        \tnull\t2021-11-07T00:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T00:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T00:00:00.000000000Z
                        PEHN\t81.46807944500559\t2021-11-07T00:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T00:00:00.000000000Z
                        \tnull\t2021-11-07T01:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T01:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T01:00:00.000000000Z
                        PEHN\t81.46807944500559\t2021-11-07T01:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T01:00:00.000000000Z
                        \t26.922103479744898\t2021-11-07T02:00:00.000000000Z
                        VTJW\t48.820511018586934\t2021-11-07T02:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T02:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T02:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T02:00:00.000000000Z
                        \t52.98405941762054\t2021-11-07T03:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T03:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T03:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T03:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T03:00:00.000000000Z
                        \tnull\t2021-11-07T04:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T04:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T04:00:00.000000000Z
                        PEHN\t84.45258177211063\t2021-11-07T04:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T04:00:00.000000000Z
                        \t97.5019885372507\t2021-11-07T05:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T05:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T05:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T05:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T05:00:00.000000000Z
                        \t80.01121139739173\t2021-11-07T06:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T06:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T06:00:00.000000000Z
                        PEHN\t49.00510449885239\t2021-11-07T06:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T06:00:00.000000000Z
                        \t92.050039469858\t2021-11-07T07:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T07:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T07:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T07:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T07:00:00.000000000Z
                        \t45.6344569609078\t2021-11-07T08:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T08:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T08:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T08:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T08:00:00.000000000Z
                        \t40.455469747939254\t2021-11-07T09:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T09:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T09:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T09:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T09:00:00.000000000Z
                        """,
                "select b, s, to_timezone(k, 'EST') k from (select b, sum(a) s, k from x sample by 1h fill(null) align to calendar time zone 'EST')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-11-06T22:10:00.000000000Z' as timestamp_ns), 3100000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                null,
                false
        );
    }

    @Test
    public void testSampleFillNullAlignToCalendarTimeZoneByte() throws Exception {
        // EST timezone has clock go back on 7 Nov. From -4 UTC to -5 UTC
        // at 6am UTC EST time is 2am (DTS), when clock goes back 7am also becomes 2am UTC
        // hence 6am UTC is duplicate timestamp and is expected to be compounded
        assertQuery(
                """
                        b\ts\tk
                        \t77\t2021-11-06T18:00:00.000000000Z
                        VTJW\t0\t2021-11-06T18:00:00.000000000Z
                        RXGZ\t0\t2021-11-06T18:00:00.000000000Z
                        PEHN\t0\t2021-11-06T18:00:00.000000000Z
                        CPSW\t0\t2021-11-06T18:00:00.000000000Z
                        HYRX\t0\t2021-11-06T18:00:00.000000000Z
                        \t36\t2021-11-06T19:00:00.000000000Z
                        VTJW\t101\t2021-11-06T19:00:00.000000000Z
                        RXGZ\t0\t2021-11-06T19:00:00.000000000Z
                        PEHN\t0\t2021-11-06T19:00:00.000000000Z
                        CPSW\t0\t2021-11-06T19:00:00.000000000Z
                        HYRX\t0\t2021-11-06T19:00:00.000000000Z
                        \t60\t2021-11-06T20:00:00.000000000Z
                        VTJW\t0\t2021-11-06T20:00:00.000000000Z
                        RXGZ\t0\t2021-11-06T20:00:00.000000000Z
                        PEHN\t0\t2021-11-06T20:00:00.000000000Z
                        CPSW\t0\t2021-11-06T20:00:00.000000000Z
                        HYRX\t0\t2021-11-06T20:00:00.000000000Z
                        \t84\t2021-11-06T21:00:00.000000000Z
                        VTJW\t0\t2021-11-06T21:00:00.000000000Z
                        RXGZ\t0\t2021-11-06T21:00:00.000000000Z
                        PEHN\t0\t2021-11-06T21:00:00.000000000Z
                        CPSW\t0\t2021-11-06T21:00:00.000000000Z
                        HYRX\t0\t2021-11-06T21:00:00.000000000Z
                        \t0\t2021-11-06T22:00:00.000000000Z
                        VTJW\t0\t2021-11-06T22:00:00.000000000Z
                        RXGZ\t93\t2021-11-06T22:00:00.000000000Z
                        PEHN\t0\t2021-11-06T22:00:00.000000000Z
                        CPSW\t0\t2021-11-06T22:00:00.000000000Z
                        HYRX\t0\t2021-11-06T22:00:00.000000000Z
                        \t98\t2021-11-06T23:00:00.000000000Z
                        VTJW\t0\t2021-11-06T23:00:00.000000000Z
                        RXGZ\t0\t2021-11-06T23:00:00.000000000Z
                        PEHN\t0\t2021-11-06T23:00:00.000000000Z
                        CPSW\t0\t2021-11-06T23:00:00.000000000Z
                        HYRX\t0\t2021-11-06T23:00:00.000000000Z
                        \t0\t2021-11-07T00:00:00.000000000Z
                        VTJW\t0\t2021-11-07T00:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T00:00:00.000000000Z
                        PEHN\t117\t2021-11-07T00:00:00.000000000Z
                        CPSW\t0\t2021-11-07T00:00:00.000000000Z
                        HYRX\t0\t2021-11-07T00:00:00.000000000Z
                        \t0\t2021-11-07T01:00:00.000000000Z
                        VTJW\t0\t2021-11-07T01:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T01:00:00.000000000Z
                        PEHN\t0\t2021-11-07T01:00:00.000000000Z
                        CPSW\t0\t2021-11-07T01:00:00.000000000Z
                        HYRX\t0\t2021-11-07T01:00:00.000000000Z
                        \t26\t2021-11-07T02:00:00.000000000Z
                        VTJW\t0\t2021-11-07T02:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T02:00:00.000000000Z
                        PEHN\t0\t2021-11-07T02:00:00.000000000Z
                        CPSW\t0\t2021-11-07T02:00:00.000000000Z
                        HYRX\t0\t2021-11-07T02:00:00.000000000Z
                        \t0\t2021-11-07T03:00:00.000000000Z
                        VTJW\t0\t2021-11-07T03:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T03:00:00.000000000Z
                        PEHN\t0\t2021-11-07T03:00:00.000000000Z
                        CPSW\t0\t2021-11-07T03:00:00.000000000Z
                        HYRX\t26\t2021-11-07T03:00:00.000000000Z
                        \t0\t2021-11-07T04:00:00.000000000Z
                        VTJW\t119\t2021-11-07T04:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T04:00:00.000000000Z
                        PEHN\t0\t2021-11-07T04:00:00.000000000Z
                        CPSW\t0\t2021-11-07T04:00:00.000000000Z
                        HYRX\t0\t2021-11-07T04:00:00.000000000Z
                        \t120\t2021-11-07T05:00:00.000000000Z
                        VTJW\t0\t2021-11-07T05:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T05:00:00.000000000Z
                        PEHN\t0\t2021-11-07T05:00:00.000000000Z
                        CPSW\t0\t2021-11-07T05:00:00.000000000Z
                        HYRX\t0\t2021-11-07T05:00:00.000000000Z
                        \t0\t2021-11-07T06:00:00.000000000Z
                        VTJW\t0\t2021-11-07T06:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T06:00:00.000000000Z
                        PEHN\t103\t2021-11-07T06:00:00.000000000Z
                        CPSW\t103\t2021-11-07T06:00:00.000000000Z
                        HYRX\t0\t2021-11-07T06:00:00.000000000Z
                        \t0\t2021-11-07T07:00:00.000000000Z
                        VTJW\t0\t2021-11-07T07:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T07:00:00.000000000Z
                        PEHN\t0\t2021-11-07T07:00:00.000000000Z
                        CPSW\t0\t2021-11-07T07:00:00.000000000Z
                        HYRX\t70\t2021-11-07T07:00:00.000000000Z
                        \t0\t2021-11-07T08:00:00.000000000Z
                        VTJW\t122\t2021-11-07T08:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T08:00:00.000000000Z
                        PEHN\t0\t2021-11-07T08:00:00.000000000Z
                        CPSW\t0\t2021-11-07T08:00:00.000000000Z
                        HYRX\t0\t2021-11-07T08:00:00.000000000Z
                        \t62\t2021-11-07T09:00:00.000000000Z
                        VTJW\t0\t2021-11-07T09:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T09:00:00.000000000Z
                        PEHN\t0\t2021-11-07T09:00:00.000000000Z
                        CPSW\t0\t2021-11-07T09:00:00.000000000Z
                        HYRX\t0\t2021-11-07T09:00:00.000000000Z
                        """,
                "select b, s, to_timezone(k, 'EST') k from (select b, first(a) s, k from x sample by 1h fill(null) align to calendar time zone 'EST')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_byte() a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-11-06T22:10:00.000000000Z' as timestamp_ns), 3100000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                null,
                false
        );
    }

    @Test
    public void testSampleFillNullAlignToCalendarTimeZoneFloat() throws Exception {
        // EST timezone has clock go back on 7 Nov. From -4 UTC to -5 UTC
        // at 6am UTC EST time is 2am (DTS), when clock goes back 7am also becomes 2am UTC
        // hence 6am UTC is duplicate timestamp and is expected to be compounded
        assertQuery(
                """
                        b\ts\tk
                        \t0.62540215\t2021-11-06T18:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T18:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T18:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T18:00:00.000000000Z
                        CPSW\tnull\t2021-11-06T18:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T18:00:00.000000000Z
                        \t0.7611029\t2021-11-06T19:00:00.000000000Z
                        VTJW\t0.5243723\t2021-11-06T19:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T19:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T19:00:00.000000000Z
                        CPSW\tnull\t2021-11-06T19:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T19:00:00.000000000Z
                        \t0.8072372\t2021-11-06T20:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T20:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T20:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T20:00:00.000000000Z
                        CPSW\tnull\t2021-11-06T20:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T20:00:00.000000000Z
                        \t0.7261136\t2021-11-06T21:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T21:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T21:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T21:00:00.000000000Z
                        CPSW\tnull\t2021-11-06T21:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T21:00:00.000000000Z
                        \tnull\t2021-11-06T22:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T22:00:00.000000000Z
                        RXGZ\t0.6276954\t2021-11-06T22:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T22:00:00.000000000Z
                        CPSW\tnull\t2021-11-06T22:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T22:00:00.000000000Z
                        \t0.67785645\t2021-11-06T23:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T23:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T23:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T23:00:00.000000000Z
                        CPSW\tnull\t2021-11-06T23:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T23:00:00.000000000Z
                        \tnull\t2021-11-07T00:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T00:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T00:00:00.000000000Z
                        PEHN\t0.31005454\t2021-11-07T00:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T00:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T00:00:00.000000000Z
                        \tnull\t2021-11-07T01:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T01:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T01:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T01:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T01:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T01:00:00.000000000Z
                        \t0.69054043\t2021-11-07T02:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T02:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T02:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T02:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T02:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T02:00:00.000000000Z
                        \tnull\t2021-11-07T03:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T03:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T03:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T03:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T03:00:00.000000000Z
                        HYRX\t0.21583223\t2021-11-07T03:00:00.000000000Z
                        \tnull\t2021-11-07T04:00:00.000000000Z
                        VTJW\t0.1578663\t2021-11-07T04:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T04:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T04:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T04:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T04:00:00.000000000Z
                        \t0.19112343\t2021-11-07T05:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T05:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T05:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T05:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T05:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T05:00:00.000000000Z
                        \tnull\t2021-11-07T06:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T06:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T06:00:00.000000000Z
                        PEHN\t0.1250304\t2021-11-07T06:00:00.000000000Z
                        CPSW\t0.90380687\t2021-11-07T06:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T06:00:00.000000000Z
                        \tnull\t2021-11-07T07:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T07:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T07:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T07:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T07:00:00.000000000Z
                        HYRX\t0.1345017\t2021-11-07T07:00:00.000000000Z
                        \tnull\t2021-11-07T08:00:00.000000000Z
                        VTJW\t0.8912587\t2021-11-07T08:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T08:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T08:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T08:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T08:00:00.000000000Z
                        \t0.97552633\t2021-11-07T09:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T09:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T09:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T09:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T09:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T09:00:00.000000000Z
                        """,
                "select b, s, to_timezone(k, 'EST') k from (select b, first(a) s, k from x sample by 1h fill(null) align to calendar time zone 'EST')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_float() a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-11-06T22:10:00.000000000Z' as timestamp_ns), 3100000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                null,
                false
        );
    }

    @Test
    public void testSampleFillNullAlignToCalendarTimeZoneInt() throws Exception {
        // EST timezone has clock go back on 7 Nov. From -4 UTC to -5 UTC
        // at 6am UTC EST time is 2am (DTS), when clock goes back 7am also becomes 2am UTC
        // hence 6am UTC is duplicate timestamp and is expected to be compounded
        assertQuery(
                """
                        b\ts\tk
                        \t1530831067\t2021-11-06T18:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T18:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T18:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T18:00:00.000000000Z
                        CPSW\tnull\t2021-11-06T18:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T18:00:00.000000000Z
                        \t-1532328444\t2021-11-06T19:00:00.000000000Z
                        VTJW\t1125579207\t2021-11-06T19:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T19:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T19:00:00.000000000Z
                        CPSW\tnull\t2021-11-06T19:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T19:00:00.000000000Z
                        \t426455968\t2021-11-06T20:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T20:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T20:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T20:00:00.000000000Z
                        CPSW\tnull\t2021-11-06T20:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T20:00:00.000000000Z
                        \t-1792928964\t2021-11-06T21:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T21:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T21:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T21:00:00.000000000Z
                        CPSW\tnull\t2021-11-06T21:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T21:00:00.000000000Z
                        \tnull\t2021-11-06T22:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T22:00:00.000000000Z
                        RXGZ\t-1520872171\t2021-11-06T22:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T22:00:00.000000000Z
                        CPSW\tnull\t2021-11-06T22:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T22:00:00.000000000Z
                        \t1404198\t2021-11-06T23:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T23:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T23:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T23:00:00.000000000Z
                        CPSW\tnull\t2021-11-06T23:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T23:00:00.000000000Z
                        \tnull\t2021-11-07T00:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T00:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T00:00:00.000000000Z
                        PEHN\t-1125169127\t2021-11-07T00:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T00:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T00:00:00.000000000Z
                        \tnull\t2021-11-07T01:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T01:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T01:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T01:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T01:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T01:00:00.000000000Z
                        \t1110979454\t2021-11-07T02:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T02:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T02:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T02:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T02:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T02:00:00.000000000Z
                        \tnull\t2021-11-07T03:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T03:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T03:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T03:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T03:00:00.000000000Z
                        HYRX\t-938514914\t2021-11-07T03:00:00.000000000Z
                        \tnull\t2021-11-07T04:00:00.000000000Z
                        VTJW\t-303295973\t2021-11-07T04:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T04:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T04:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T04:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T04:00:00.000000000Z
                        \t2006313928\t2021-11-07T05:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T05:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T05:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T05:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T05:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T05:00:00.000000000Z
                        \tnull\t2021-11-07T06:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T06:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T06:00:00.000000000Z
                        PEHN\t-27395319\t2021-11-07T06:00:00.000000000Z
                        CPSW\t-483853667\t2021-11-07T06:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T06:00:00.000000000Z
                        \tnull\t2021-11-07T07:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T07:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T07:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T07:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T07:00:00.000000000Z
                        HYRX\t-1272693194\t2021-11-07T07:00:00.000000000Z
                        \tnull\t2021-11-07T08:00:00.000000000Z
                        VTJW\t-2002373666\t2021-11-07T08:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T08:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T08:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T08:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T08:00:00.000000000Z
                        \t410717394\t2021-11-07T09:00:00.000000000Z
                        VTJW\tnull\t2021-11-07T09:00:00.000000000Z
                        RXGZ\tnull\t2021-11-07T09:00:00.000000000Z
                        PEHN\tnull\t2021-11-07T09:00:00.000000000Z
                        CPSW\tnull\t2021-11-07T09:00:00.000000000Z
                        HYRX\tnull\t2021-11-07T09:00:00.000000000Z
                        """,
                "select b, s, to_timezone(k, 'EST') k from (select b, first(a) s, k from x sample by 1h fill(null) align to calendar time zone 'EST')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_int() a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-11-06T22:10:00.000000000Z' as timestamp_ns), 3100000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                null,
                false
        );
    }

    @Test
    public void testSampleFillNullAlignToCalendarTimeZoneShort() throws Exception {
        // EST timezone has clock go back on 7 Nov. From -4 UTC to -5 UTC
        // at 6am UTC EST time is 2am (DTS), when clock goes back 7am also becomes 2am UTC
        // hence 6am UTC is duplicate timestamp and is expected to be compounded
        assertQuery(
                """
                        b\ts\tk
                        \t-24357\t2021-11-06T18:00:00.000000000Z
                        VTJW\t0\t2021-11-06T18:00:00.000000000Z
                        RXGZ\t0\t2021-11-06T18:00:00.000000000Z
                        PEHN\t0\t2021-11-06T18:00:00.000000000Z
                        CPSW\t0\t2021-11-06T18:00:00.000000000Z
                        HYRX\t0\t2021-11-06T18:00:00.000000000Z
                        \t-31228\t2021-11-06T19:00:00.000000000Z
                        VTJW\t-1593\t2021-11-06T19:00:00.000000000Z
                        RXGZ\t0\t2021-11-06T19:00:00.000000000Z
                        PEHN\t0\t2021-11-06T19:00:00.000000000Z
                        CPSW\t0\t2021-11-06T19:00:00.000000000Z
                        HYRX\t0\t2021-11-06T19:00:00.000000000Z
                        \t13216\t2021-11-06T20:00:00.000000000Z
                        VTJW\t0\t2021-11-06T20:00:00.000000000Z
                        RXGZ\t0\t2021-11-06T20:00:00.000000000Z
                        PEHN\t0\t2021-11-06T20:00:00.000000000Z
                        CPSW\t0\t2021-11-06T20:00:00.000000000Z
                        HYRX\t0\t2021-11-06T20:00:00.000000000Z
                        \t4924\t2021-11-06T21:00:00.000000000Z
                        VTJW\t0\t2021-11-06T21:00:00.000000000Z
                        RXGZ\t0\t2021-11-06T21:00:00.000000000Z
                        PEHN\t0\t2021-11-06T21:00:00.000000000Z
                        CPSW\t0\t2021-11-06T21:00:00.000000000Z
                        HYRX\t0\t2021-11-06T21:00:00.000000000Z
                        \t0\t2021-11-06T22:00:00.000000000Z
                        VTJW\t0\t2021-11-06T22:00:00.000000000Z
                        RXGZ\t21781\t2021-11-06T22:00:00.000000000Z
                        PEHN\t0\t2021-11-06T22:00:00.000000000Z
                        CPSW\t0\t2021-11-06T22:00:00.000000000Z
                        HYRX\t0\t2021-11-06T22:00:00.000000000Z
                        \t27942\t2021-11-06T23:00:00.000000000Z
                        VTJW\t0\t2021-11-06T23:00:00.000000000Z
                        RXGZ\t0\t2021-11-06T23:00:00.000000000Z
                        PEHN\t0\t2021-11-06T23:00:00.000000000Z
                        CPSW\t0\t2021-11-06T23:00:00.000000000Z
                        HYRX\t0\t2021-11-06T23:00:00.000000000Z
                        \t0\t2021-11-07T00:00:00.000000000Z
                        VTJW\t0\t2021-11-07T00:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T00:00:00.000000000Z
                        PEHN\t18457\t2021-11-07T00:00:00.000000000Z
                        CPSW\t0\t2021-11-07T00:00:00.000000000Z
                        HYRX\t0\t2021-11-07T00:00:00.000000000Z
                        \t0\t2021-11-07T01:00:00.000000000Z
                        VTJW\t0\t2021-11-07T01:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T01:00:00.000000000Z
                        PEHN\t0\t2021-11-07T01:00:00.000000000Z
                        CPSW\t0\t2021-11-07T01:00:00.000000000Z
                        HYRX\t0\t2021-11-07T01:00:00.000000000Z
                        \t13182\t2021-11-07T02:00:00.000000000Z
                        VTJW\t0\t2021-11-07T02:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T02:00:00.000000000Z
                        PEHN\t0\t2021-11-07T02:00:00.000000000Z
                        CPSW\t0\t2021-11-07T02:00:00.000000000Z
                        HYRX\t0\t2021-11-07T02:00:00.000000000Z
                        \t0\t2021-11-07T03:00:00.000000000Z
                        VTJW\t0\t2021-11-07T03:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T03:00:00.000000000Z
                        PEHN\t0\t2021-11-07T03:00:00.000000000Z
                        CPSW\t0\t2021-11-07T03:00:00.000000000Z
                        HYRX\t26142\t2021-11-07T03:00:00.000000000Z
                        \t0\t2021-11-07T04:00:00.000000000Z
                        VTJW\t4635\t2021-11-07T04:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T04:00:00.000000000Z
                        PEHN\t0\t2021-11-07T04:00:00.000000000Z
                        CPSW\t0\t2021-11-07T04:00:00.000000000Z
                        HYRX\t0\t2021-11-07T04:00:00.000000000Z
                        \t-5176\t2021-11-07T05:00:00.000000000Z
                        VTJW\t0\t2021-11-07T05:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T05:00:00.000000000Z
                        PEHN\t0\t2021-11-07T05:00:00.000000000Z
                        CPSW\t0\t2021-11-07T05:00:00.000000000Z
                        HYRX\t0\t2021-11-07T05:00:00.000000000Z
                        \t0\t2021-11-07T06:00:00.000000000Z
                        VTJW\t0\t2021-11-07T06:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T06:00:00.000000000Z
                        PEHN\t-1271\t2021-11-07T06:00:00.000000000Z
                        CPSW\t-1379\t2021-11-07T06:00:00.000000000Z
                        HYRX\t0\t2021-11-07T06:00:00.000000000Z
                        \t0\t2021-11-07T07:00:00.000000000Z
                        VTJW\t0\t2021-11-07T07:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T07:00:00.000000000Z
                        PEHN\t0\t2021-11-07T07:00:00.000000000Z
                        CPSW\t0\t2021-11-07T07:00:00.000000000Z
                        HYRX\t15926\t2021-11-07T07:00:00.000000000Z
                        \t0\t2021-11-07T08:00:00.000000000Z
                        VTJW\t13278\t2021-11-07T08:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T08:00:00.000000000Z
                        PEHN\t0\t2021-11-07T08:00:00.000000000Z
                        CPSW\t0\t2021-11-07T08:00:00.000000000Z
                        HYRX\t0\t2021-11-07T08:00:00.000000000Z
                        \t3282\t2021-11-07T09:00:00.000000000Z
                        VTJW\t0\t2021-11-07T09:00:00.000000000Z
                        RXGZ\t0\t2021-11-07T09:00:00.000000000Z
                        PEHN\t0\t2021-11-07T09:00:00.000000000Z
                        CPSW\t0\t2021-11-07T09:00:00.000000000Z
                        HYRX\t0\t2021-11-07T09:00:00.000000000Z
                        """,
                "select b, s, to_timezone(k, 'EST') k from (select b, first(a) s, k from x sample by 1h fill(null) align to calendar time zone 'EST')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_short() a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-11-06T22:10:00.000000000Z' as timestamp_ns), 3100000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                null,
                false
        );
    }

    @Test
    public void testSampleFillNullBadType() throws Exception {
        assertException(
                "select b, sum_t(b), k from x sample by 3h fill(null)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_str(1,1,2) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                10,
                "Unsupported type"
        );
    }

    @Test
    public void testSampleFillNullBadTypeSequential() throws Exception {
        assertException(
                "select b, sum_t(b), k from x sample by 3h fill(null)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_str(1,1,2) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                10,
                "Unsupported type"
        );
    }

    @Test
    public void testSampleFillNullDay() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        \t7275.778376911272\t1970-01-03T00:00:00.000000000Z
                        VTJW\t1883.352722741196\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t1778.991207981299\t1970-01-03T00:00:00.000000000Z
                        PEHN\t1320.0312922751193\t1970-01-03T00:00:00.000000000Z
                        HYRX\t1331.6811166028579\t1970-01-03T00:00:00.000000000Z
                        CPSW\t1028.7651538511032\t1970-01-03T00:00:00.000000000Z
                        \t3197.327071423042\t1970-01-15T00:00:00.000000000Z
                        VTJW\t620.7711228918114\t1970-01-15T00:00:00.000000000Z
                        RXGZ\t352.08258484411346\t1970-01-15T00:00:00.000000000Z
                        PEHN\t535.1155923549986\t1970-01-15T00:00:00.000000000Z
                        HYRX\t646.1950909401153\t1970-01-15T00:00:00.000000000Z
                        CPSW\t751.4428172676351\t1970-01-15T00:00:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 12d fill(null) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(400)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillNullDayNotKeyed() throws Exception {
        assertQuery(
                """
                        sum\tk
                        14618.599870362843\t1970-01-03T00:00:00.000000000Z
                        6102.934279721718\t1970-01-15T00:00:00.000000000Z
                        """,
                "select sum(a), k from x sample by 12d fill(null) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(400)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillNullDayNotKeyedGaps() throws Exception {
        assertQuery(
                """
                        sum\tk
                        11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        null\t1970-01-04T00:00:00.000000000Z
                        42.17768841969397\t1970-01-05T00:00:00.000000000Z
                        null\t1970-01-06T00:00:00.000000000Z
                        23.90529010846525\t1970-01-07T00:00:00.000000000Z
                        null\t1970-01-08T00:00:00.000000000Z
                        70.94360487171201\t1970-01-09T00:00:00.000000000Z
                        null\t1970-01-10T00:00:00.000000000Z
                        87.99634725391621\t1970-01-11T00:00:00.000000000Z
                        null\t1970-01-12T00:00:00.000000000Z
                        32.881769076795045\t1970-01-13T00:00:00.000000000Z
                        null\t1970-01-14T00:00:00.000000000Z
                        97.71103146051203\t1970-01-15T00:00:00.000000000Z
                        null\t1970-01-16T00:00:00.000000000Z
                        81.46807944500559\t1970-01-17T00:00:00.000000000Z
                        null\t1970-01-18T00:00:00.000000000Z
                        57.93466326862211\t1970-01-19T00:00:00.000000000Z
                        null\t1970-01-20T00:00:00.000000000Z
                        12.026122412833129\t1970-01-21T00:00:00.000000000Z
                        null\t1970-01-22T00:00:00.000000000Z
                        48.820511018586934\t1970-01-23T00:00:00.000000000Z
                        null\t1970-01-24T00:00:00.000000000Z
                        26.922103479744898\t1970-01-25T00:00:00.000000000Z
                        null\t1970-01-26T00:00:00.000000000Z
                        52.98405941762054\t1970-01-27T00:00:00.000000000Z
                        null\t1970-01-28T00:00:00.000000000Z
                        84.45258177211063\t1970-01-29T00:00:00.000000000Z
                        null\t1970-01-30T00:00:00.000000000Z
                        97.5019885372507\t1970-01-31T00:00:00.000000000Z
                        null\t1970-02-01T00:00:00.000000000Z
                        49.00510449885239\t1970-02-02T00:00:00.000000000Z
                        null\t1970-02-03T00:00:00.000000000Z
                        80.01121139739173\t1970-02-04T00:00:00.000000000Z
                        null\t1970-02-05T00:00:00.000000000Z
                        92.050039469858\t1970-02-06T00:00:00.000000000Z
                        null\t1970-02-07T00:00:00.000000000Z
                        45.6344569609078\t1970-02-08T00:00:00.000000000Z
                        null\t1970-02-09T00:00:00.000000000Z
                        40.455469747939254\t1970-02-10T00:00:00.000000000Z
                        """,
                "select sum(a), k from x sample by 1d fill(null)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 2*24*3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                true
        );
    }

    @Test
    public void testSampleFillNullMonth() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        \t21134.785865526985\t2020-01-31T00:15:00.000000000Z
                        VTJW\t4107.88003812462\t2020-01-31T00:15:00.000000000Z
                        RXGZ\t3878.5796128414113\t2020-01-31T00:15:00.000000000Z
                        PEHN\t3830.3214041454585\t2020-01-31T00:15:00.000000000Z
                        HYRX\t4022.090049039533\t2020-01-31T00:15:00.000000000Z
                        CPSW\t3809.0167417062717\t2020-01-31T00:15:00.000000000Z
                        \t21992.40312469368\t2020-02-29T00:15:00.000000000Z
                        VTJW\t4269.876322655899\t2020-02-29T00:15:00.000000000Z
                        RXGZ\t3873.962076571017\t2020-02-29T00:15:00.000000000Z
                        PEHN\t5188.519914927756\t2020-02-29T00:15:00.000000000Z
                        HYRX\t4827.485503185921\t2020-02-29T00:15:00.000000000Z
                        CPSW\t3649.9322065714528\t2020-02-29T00:15:00.000000000Z
                        \t13015.370507317415\t2020-03-31T00:15:00.000000000Z
                        VTJW\t3512.540418016438\t2020-03-31T00:15:00.000000000Z
                        RXGZ\t2721.6299830012695\t2020-03-31T00:15:00.000000000Z
                        PEHN\t2383.9330634058742\t2020-03-31T00:15:00.000000000Z
                        HYRX\t2717.9604384639747\t2020-03-31T00:15:00.000000000Z
                        CPSW\t2296.4189057500093\t2020-03-31T00:15:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 1M fill(null) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2020-01-31T00:15:00.000000000Z' as timestamp_ns), 3100000000000) k" +
                        " from" +
                        " long_sequence(2200)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillNullMonthAlignToCalendar() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        \t752.2523117908589\t2020-01-01T00:00:00.000000000Z
                        VTJW\t177.84974249247676\t2020-01-01T00:00:00.000000000Z
                        RXGZ\t80.49958150707765\t2020-01-01T00:00:00.000000000Z
                        PEHN\t297.9750008612368\t2020-01-01T00:00:00.000000000Z
                        HYRX\t159.16605899292972\t2020-01-01T00:00:00.000000000Z
                        CPSW\t12.02416087573498\t2020-01-01T00:00:00.000000000Z
                        \t20982.992943772188\t2020-02-01T00:00:00.000000000Z
                        VTJW\t4052.7570641301963\t2020-02-01T00:00:00.000000000Z
                        RXGZ\t4063.6346983106814\t2020-02-01T00:00:00.000000000Z
                        PEHN\t3822.787218703387\t2020-02-01T00:00:00.000000000Z
                        HYRX\t4024.0173141255395\t2020-02-01T00:00:00.000000000Z
                        CPSW\t3857.95047972562\t2020-02-01T00:00:00.000000000Z
                        \t22044.41664067389\t2020-03-01T00:00:00.000000000Z
                        VTJW\t4217.430402136565\t2020-03-01T00:00:00.000000000Z
                        RXGZ\t3810.211911971742\t2020-03-01T00:00:00.000000000Z
                        PEHN\t4978.97740950064\t2020-03-01T00:00:00.000000000Z
                        HYRX\t4829.549935728278\t2020-03-01T00:00:00.000000000Z
                        CPSW\t3600.963491125638\t2020-03-01T00:00:00.000000000Z
                        \t12362.897601301143\t2020-04-01T00:00:00.000000000Z
                        VTJW\t3442.2595700377174\t2020-04-01T00:00:00.000000000Z
                        RXGZ\t2519.8254806241976\t2020-04-01T00:00:00.000000000Z
                        PEHN\t2303.0347534138255\t2020-04-01T00:00:00.000000000Z
                        HYRX\t2554.8026818426815\t2020-04-01T00:00:00.000000000Z
                        CPSW\t2284.4297223007393\t2020-04-01T00:00:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 1M fill(null) align to calendar",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2020-01-31T00:15:00.000000000Z' as timestamp_ns), 3100000000000) k" +
                        " from" +
                        " long_sequence(2200)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillNullNotKeyedAlignToCalendar() throws Exception {
        assertQuery(
                """
                        s\tk
                        0.15786635599554755\t2021-10-31T02:00:00.000000000Z
                        0.7166790794135658\t2021-10-31T02:30:00.000000000Z
                        null\t2021-10-31T03:30:00.000000000Z
                        null\t2021-10-31T04:00:00.000000000Z
                        0.22631523434159562\t2021-10-31T04:30:00.000000000Z
                        null\t2021-10-31T05:00:00.000000000Z
                        0.6940904779678791\t2021-10-31T05:30:00.000000000Z
                        null\t2021-10-31T06:00:00.000000000Z
                        0.5913874468544745\t2021-10-31T06:30:00.000000000Z
                        null\t2021-10-31T07:00:00.000000000Z
                        0.04001697462715281\t2021-10-31T07:30:00.000000000Z
                        null\t2021-10-31T08:00:00.000000000Z
                        0.07828020681514525\t2021-10-31T08:30:00.000000000Z
                        null\t2021-10-31T09:00:00.000000000Z
                        null\t2021-10-31T09:30:00.000000000Z
                        0.7431472218131966\t2021-10-31T10:00:00.000000000Z
                        null\t2021-10-31T10:30:00.000000000Z
                        0.13312214396754163\t2021-10-31T11:00:00.000000000Z
                        null\t2021-10-31T11:30:00.000000000Z
                        null\t2021-10-31T12:00:00.000000000Z
                        null\t2021-10-31T12:30:00.000000000Z
                        0.2325041018786207\t2021-10-31T13:00:00.000000000Z
                        null\t2021-10-31T13:30:00.000000000Z
                        0.8853675629694284\t2021-10-31T14:00:00.000000000Z
                        null\t2021-10-31T14:30:00.000000000Z
                        0.6940917925148332\t2021-10-31T15:00:00.000000000Z
                        null\t2021-10-31T15:30:00.000000000Z
                        0.4031733414086601\t2021-10-31T16:00:00.000000000Z
                        null\t2021-10-31T16:30:00.000000000Z
                        0.27755720049807464\t2021-10-31T17:00:00.000000000Z
                        null\t2021-10-31T17:30:00.000000000Z
                        0.6361737673041902\t2021-10-31T18:00:00.000000000Z
                        0.5965069739835686\t2021-10-31T18:30:00.000000000Z
                        null\t2021-10-31T19:00:00.000000000Z
                        null\t2021-10-31T19:30:00.000000000Z
                        null\t2021-10-31T20:00:00.000000000Z
                        null\t2021-10-31T20:30:00.000000000Z
                        null\t2021-10-31T21:00:00.000000000Z
                        0.5785645380474713\t2021-10-31T21:30:00.000000000Z
                        null\t2021-10-31T22:00:00.000000000Z
                        0.7291265477629812\t2021-10-31T22:30:00.000000000Z
                        null\t2021-10-31T23:00:00.000000000Z
                        0.8642800031609658\t2021-10-31T23:30:00.000000000Z
                        null\t2021-11-01T00:00:00.000000000Z
                        null\t2021-11-01T00:30:00.000000000Z
                        null\t2021-11-01T01:00:00.000000000Z
                        0.8925004728084927\t2021-11-01T01:30:00.000000000Z
                        null\t2021-11-01T02:00:00.000000000Z
                        0.5522442336842381\t2021-11-01T02:30:00.000000000Z
                        null\t2021-11-01T03:00:00.000000000Z
                        null\t2021-11-01T03:30:00.000000000Z
                        0.7504512900310369\t2021-11-01T04:00:00.000000000Z
                        """,
                "select s, to_timezone(k, 'Europe/Berlin') k from (select sum(o) s, k from x sample by 30m fill(null) align to calendar time zone 'Europe/Berlin')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_int() a," +
                        " rnd_boolean() b," +
                        " rnd_str(1,1,2) c," +
                        " rnd_double(2) d," +
                        " rnd_float(2) e," +
                        " rnd_short(10,1024) f," +
                        " rnd_date(to_date('2015', 'yyyy'), to_date('2016', 'yyyy'), 2) g," +
                        " rnd_symbol(4,4,4,2) i," +
                        " rnd_long() j," +
                        " rnd_byte(2,50) l," +
                        " rnd_bin(10, 20, 2) m," +
                        " rnd_str(5,16,2) n," +
                        " rnd_double(2) o," +
                        " timestamp_sequence_ns(cast('2020-03-28T03:20:00.000000000Z' as timestamp_ns), 3600000000000) p," +
                        " timestamp_sequence_ns(cast('2021-10-31T00:00:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(30)" +
                        ") timestamp(k) partition by NONE",
                null,
                false,
                false
        );
    }

    @Test
    public void testSampleFillNullNotKeyedEmpty() throws Exception {
        assertQuery(
                "sum\tk\n",
                "select sum(a), k from x sample by 3h fill(null) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(0)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        sum\tk
                        139.2898345080353\t1970-01-04T05:00:00.000000000Z
                        121.75073858040724\t1970-01-04T08:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillNullNotKeyedInvalidSequential() throws Exception {
        assertException(
                "select last(z) s from x sample by 30m fill(null) align to calendar with offset '10:00'",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_int() a," +
                        " rnd_boolean() b," +
                        " rnd_str(1,1,2) c," +
                        " rnd_double(2) d," +
                        " rnd_float(2) e," +
                        " rnd_short(10,1024) f," +
                        " rnd_date(to_date('2015', 'yyyy'), to_date('2016', 'yyyy'), 2) g," +
                        " rnd_symbol(4,4,4,2) i," +
                        " rnd_long() j," +
                        " rnd_byte(2,50) l," +
                        " rnd_bin(10, 20, 2) m," +
                        " rnd_str(5,16,2) n," +
                        " rnd_double(2) o," +
                        " rnd_char() z," +
                        " rnd_varchar(5, 16, 2) vch," +
                        " timestamp_sequence_ns(cast('2020-03-28T03:20:00.000000000Z' as timestamp_ns), 3600000000000) p," +
                        " timestamp_sequence_ns(cast('2021-10-31T00:00:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(30)" +
                        ") timestamp(k) partition by NONE",
                7,
                "Unsupported type: CHAR"
        );
    }

    @Test
    public void testSampleFillNullNotKeyedValid() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table x as " +
                    "(" +
                    "select" +
                    " rnd_int() a," +
                    " rnd_boolean() b," +
                    " rnd_str(1,1,2) c," +
                    " rnd_double(2) d," +
                    " rnd_float(2) e," +
                    " rnd_short(10,1024) f," +
                    " rnd_date(to_date('2015', 'yyyy'), to_date('2016', 'yyyy'), 2) g," +
                    " rnd_symbol(4,4,4,2) i," +
                    " rnd_long() j," +
                    " rnd_byte(2,50) l," +
                    " rnd_bin(10, 20, 2) m," +
                    " rnd_str(5,16,2) n," +
                    " rnd_double(2) o," +
                    " rnd_char() z," +
                    " rnd_varchar(5, 16, 2) vch," +
                    " timestamp_sequence_ns(cast('2020-03-28T03:20:00.000000000Z' as timestamp_ns), 3600000000000) p," +
                    " timestamp_sequence_ns(cast('2021-10-31T00:00:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                    " from" +
                    " long_sequence(30)" +
                    ") timestamp(k) partition by NONE");

            assertPlanNoLeakCheck(
                    "select last(z) s from x sample by 30m fill(null)",
                    """
                            SelectedRecord
                                Sort
                                  keys: [k]
                                    Fill Range
                                      stride: '30m'
                                      values: [null]
                                        Async Group By workers: 1
                                          keys: [k]
                                          values: [last(z)]
                                          filter: null
                                            PageFrame
                                                Row forward scan
                                                Frame forward scan on: x
                            """
            );
        });
    }

    @Test
    public void testSampleFillNullYear() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        \t230471.6324115649\t2020-02-29T00:15:00.000000000Z
                        VTJW\t46973.91444645728\t2020-02-29T00:15:00.000000000Z
                        RXGZ\t48910.17324550927\t2020-02-29T00:15:00.000000000Z
                        PEHN\t48583.20070970916\t2020-02-29T00:15:00.000000000Z
                        HYRX\t46537.917653652206\t2020-02-29T00:15:00.000000000Z
                        CPSW\t46524.67428412209\t2020-02-29T00:15:00.000000000Z
                        \t231249.76682044208\t2021-02-28T00:15:00.000000000Z
                        VTJW\t45621.42143778673\t2021-02-28T00:15:00.000000000Z
                        RXGZ\t45567.34528840918\t2021-02-28T00:15:00.000000000Z
                        PEHN\t47861.85423999244\t2021-02-28T00:15:00.000000000Z
                        HYRX\t46594.345442832084\t2021-02-28T00:15:00.000000000Z
                        CPSW\t46438.25199411636\t2021-02-28T00:15:00.000000000Z
                        \t232812.52161013582\t2022-02-28T00:15:00.000000000Z
                        VTJW\t46811.48077873179\t2022-02-28T00:15:00.000000000Z
                        RXGZ\t45621.374911602004\t2022-02-28T00:15:00.000000000Z
                        PEHN\t48082.53111438876\t2022-02-28T00:15:00.000000000Z
                        HYRX\t46863.153171237995\t2022-02-28T00:15:00.000000000Z
                        CPSW\t44593.48699070174\t2022-02-28T00:15:00.000000000Z
                        \t233215.37587268485\t2023-02-28T00:15:00.000000000Z
                        VTJW\t44204.09263507446\t2023-02-28T00:15:00.000000000Z
                        RXGZ\t50308.62007020325\t2023-02-28T00:15:00.000000000Z
                        PEHN\t49181.81093939917\t2023-02-28T00:15:00.000000000Z
                        HYRX\t45520.372793862916\t2023-02-28T00:15:00.000000000Z
                        CPSW\t46341.68837957697\t2023-02-28T00:15:00.000000000Z
                        \t233714.05097628923\t2024-02-29T00:15:00.000000000Z
                        VTJW\t45484.161487529505\t2024-02-29T00:15:00.000000000Z
                        RXGZ\t46259.36048949232\t2024-02-29T00:15:00.000000000Z
                        PEHN\t46788.82589169763\t2024-02-29T00:15:00.000000000Z
                        HYRX\t41880.23420937183\t2024-02-29T00:15:00.000000000Z
                        CPSW\t47922.072692300695\t2024-02-29T00:15:00.000000000Z
                        \t236927.6092092266\t2025-02-28T00:15:00.000000000Z
                        VTJW\t42633.66068991962\t2025-02-28T00:15:00.000000000Z
                        RXGZ\t46016.35170858719\t2025-02-28T00:15:00.000000000Z
                        PEHN\t47202.74603066283\t2025-02-28T00:15:00.000000000Z
                        HYRX\t42400.67950374062\t2025-02-28T00:15:00.000000000Z
                        CPSW\t45738.53724930489\t2025-02-28T00:15:00.000000000Z
                        \t237442.88497528585\t2026-02-28T00:15:00.000000000Z
                        VTJW\t43843.95040100638\t2026-02-28T00:15:00.000000000Z
                        RXGZ\t45239.271502515905\t2026-02-28T00:15:00.000000000Z
                        PEHN\t45607.78533946278\t2026-02-28T00:15:00.000000000Z
                        HYRX\t45142.96124768768\t2026-02-28T00:15:00.000000000Z
                        CPSW\t46990.10882605937\t2026-02-28T00:15:00.000000000Z
                        \t232826.0235356171\t2027-02-28T00:15:00.000000000Z
                        VTJW\t48668.669793590445\t2027-02-28T00:15:00.000000000Z
                        RXGZ\t46448.54185201239\t2027-02-28T00:15:00.000000000Z
                        PEHN\t42943.08402190233\t2027-02-28T00:15:00.000000000Z
                        HYRX\t46537.48255775716\t2027-02-28T00:15:00.000000000Z
                        CPSW\t45187.64036972609\t2027-02-28T00:15:00.000000000Z
                        \t234645.69691210115\t2028-02-29T00:15:00.000000000Z
                        VTJW\t42705.103178018166\t2028-02-29T00:15:00.000000000Z
                        RXGZ\t46022.75968121836\t2028-02-29T00:15:00.000000000Z
                        PEHN\t45641.53300759042\t2028-02-29T00:15:00.000000000Z
                        HYRX\t48994.94088725773\t2028-02-29T00:15:00.000000000Z
                        CPSW\t45298.613588201646\t2028-02-29T00:15:00.000000000Z
                        \t232034.32216957968\t2029-02-28T00:15:00.000000000Z
                        VTJW\t46805.21601286143\t2029-02-28T00:15:00.000000000Z
                        RXGZ\t42698.200674677464\t2029-02-28T00:15:00.000000000Z
                        PEHN\t47587.847710706155\t2029-02-28T00:15:00.000000000Z
                        HYRX\t48486.786734451525\t2029-02-28T00:15:00.000000000Z
                        CPSW\t46641.65417643289\t2029-02-28T00:15:00.000000000Z
                        \t231387.5012794455\t2030-02-28T00:15:00.000000000Z
                        VTJW\t44880.62331960856\t2030-02-28T00:15:00.000000000Z
                        RXGZ\t46543.914680140944\t2030-02-28T00:15:00.000000000Z
                        PEHN\t46209.487707690656\t2030-02-28T00:15:00.000000000Z
                        HYRX\t46795.629082771455\t2030-02-28T00:15:00.000000000Z
                        CPSW\t45491.73475954181\t2030-02-28T00:15:00.000000000Z
                        \t230437.67702762267\t2031-02-28T00:15:00.000000000Z
                        VTJW\t45641.12803989703\t2031-02-28T00:15:00.000000000Z
                        RXGZ\t46155.449451226574\t2031-02-28T00:15:00.000000000Z
                        PEHN\t48440.42784098585\t2031-02-28T00:15:00.000000000Z
                        HYRX\t44731.96784133683\t2031-02-28T00:15:00.000000000Z
                        CPSW\t44578.15877938773\t2031-02-28T00:15:00.000000000Z
                        \t212388.96018111694\t2032-02-29T00:15:00.000000000Z
                        VTJW\t43724.97766236034\t2032-02-29T00:15:00.000000000Z
                        RXGZ\t45487.68898083253\t2032-02-29T00:15:00.000000000Z
                        PEHN\t45546.76730599092\t2032-02-29T00:15:00.000000000Z
                        HYRX\t43280.419728026056\t2032-02-29T00:15:00.000000000Z
                        CPSW\t39831.67609134073\t2032-02-29T00:15:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 1y fill(null) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2020-02-29T00:15:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(120000)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillNullYearAlignToCalendar() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        \t192977.39674906916\t2020-01-01T00:00:00.000000000Z
                        VTJW\t38819.17900889909\t2020-01-01T00:00:00.000000000Z
                        RXGZ\t41231.89396856814\t2020-01-01T00:00:00.000000000Z
                        PEHN\t41154.26693020971\t2020-01-01T00:00:00.000000000Z
                        HYRX\t38585.79005800996\t2020-01-01T00:00:00.000000000Z
                        CPSW\t39995.23576507835\t2020-01-01T00:00:00.000000000Z
                        \t230201.98037264214\t2021-01-01T00:00:00.000000000Z
                        VTJW\t46031.90714575466\t2021-01-01T00:00:00.000000000Z
                        RXGZ\t45630.841778811395\t2021-01-01T00:00:00.000000000Z
                        PEHN\t48339.24529983588\t2021-01-01T00:00:00.000000000Z
                        HYRX\t45639.52581429897\t2021-01-01T00:00:00.000000000Z
                        CPSW\t46157.93033664501\t2021-01-01T00:00:00.000000000Z
                        \t232716.0885252789\t2022-01-01T00:00:00.000000000Z
                        VTJW\t47672.22031275194\t2022-01-01T00:00:00.000000000Z
                        RXGZ\t46381.871992904176\t2022-01-01T00:00:00.000000000Z
                        PEHN\t47004.12204168089\t2022-01-01T00:00:00.000000000Z
                        HYRX\t47593.89994935438\t2022-01-01T00:00:00.000000000Z
                        CPSW\t44254.55436645622\t2022-01-01T00:00:00.000000000Z
                        \t92574.9318100312\t2023-01-01T00:00:00.000000000Z
                        VTJW\t15913.049745259726\t2023-01-01T00:00:00.000000000Z
                        RXGZ\t19292.04735492167\t2023-01-01T00:00:00.000000000Z
                        PEHN\t19107.977604407188\t2023-01-01T00:00:00.000000000Z
                        HYRX\t19887.875742375127\t2023-01-01T00:00:00.000000000Z
                        CPSW\t18169.52371202805\t2023-01-01T00:00:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 1y fill(null) align to calendar",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2020-02-29T00:15:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(30000)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillPrev() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\tnull\t1970-01-03T00:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T00:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T03:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T06:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T09:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T12:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T12:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T15:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T15:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T18:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T18:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T18:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T18:00:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 3h fill(prev)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\tnull\t1970-01-03T00:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T00:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T00:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T00:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T03:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T03:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T03:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T06:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T06:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T06:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T09:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T09:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T12:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T12:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T12:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T12:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T15:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T15:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T15:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T15:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T18:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T18:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T18:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T18:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T18:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T18:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T21:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T21:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T21:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T21:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T21:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T21:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T21:00:00.000000000Z
                        \t86.08992670884706\t1970-01-04T00:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T00:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T00:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T00:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T00:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T00:00:00.000000000Z
                        \t54.49155021518948\t1970-01-04T03:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T03:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T03:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T03:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T03:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T03:00:00.000000000Z
                        \t135.835983782176\t1970-01-04T06:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T06:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T06:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T06:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T06:00:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T06:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T06:00:00.000000000Z
                        \t135.835983782176\t1970-01-04T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T09:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T09:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T09:00:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T09:00:00.000000000Z
                        KGHV\t67.52509547112409\t1970-01-04T09:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillPrevAlignToCalendar() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T00:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T00:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T03:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T06:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T06:00:00.000000000Z
                        \t79.90616289736545\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T09:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        \t269.56323940450045\t1970-01-03T12:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T12:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T12:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T12:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T12:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T15:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T15:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T15:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T15:00:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 3h fill(prev) align to calendar",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3100000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3200000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T00:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T00:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T00:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T00:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T03:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T03:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T03:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T06:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T06:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T06:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T06:00:00.000000000Z
                        \t79.90616289736545\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T09:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T09:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T09:00:00.000000000Z
                        \t269.56323940450045\t1970-01-03T12:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T12:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T12:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T12:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T12:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T12:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T12:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T15:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T15:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T15:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T15:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T15:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T18:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T18:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T18:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T18:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T18:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T18:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T21:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T21:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T21:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T21:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T21:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T21:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T21:00:00.000000000Z
                        \t86.08992670884706\t1970-01-04T00:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T00:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T00:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T00:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T00:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T00:00:00.000000000Z
                        \t131.41536915856756\t1970-01-04T03:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T03:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T03:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T03:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T03:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T03:00:00.000000000Z
                        \t58.912164838797885\t1970-01-04T06:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T06:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T06:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T06:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T06:00:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T06:00:00.000000000Z
                        KGHV\t67.52509547112409\t1970-01-04T06:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillPrevAlignToCalendarTimeZone() throws Exception {
        // EST timezone has clock go back on 7 Nov. From -4 UTC to -5 UTC
        // at 6am UTC EST time is 2am (DTS), when clock goes back 7am also becomes 2am UTC
        // hence 6am UTC is duplicate timestamp and is expected to be compounded
        assertQuery(
                """
                        b\ts\tk
                        \t11.427984775756228\t2021-11-06T18:00:00.000000000Z
                        VTJW\tnull\t2021-11-06T18:00:00.000000000Z
                        RXGZ\tnull\t2021-11-06T18:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T18:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T18:00:00.000000000Z
                        \t11.427984775756228\t2021-11-06T19:00:00.000000000Z
                        VTJW\t42.17768841969397\t2021-11-06T19:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-06T19:00:00.000000000Z
                        PEHN\tnull\t2021-11-06T19:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T19:00:00.000000000Z
                        \t11.427984775756228\t2021-11-06T20:00:00.000000000Z
                        VTJW\t42.17768841969397\t2021-11-06T20:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-06T20:00:00.000000000Z
                        PEHN\t70.94360487171201\t2021-11-06T20:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T20:00:00.000000000Z
                        \t87.99634725391621\t2021-11-06T21:00:00.000000000Z
                        VTJW\t42.17768841969397\t2021-11-06T21:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-06T21:00:00.000000000Z
                        PEHN\t70.94360487171201\t2021-11-06T21:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T21:00:00.000000000Z
                        \t32.881769076795045\t2021-11-06T22:00:00.000000000Z
                        VTJW\t42.17768841969397\t2021-11-06T22:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-06T22:00:00.000000000Z
                        PEHN\t70.94360487171201\t2021-11-06T22:00:00.000000000Z
                        HYRX\tnull\t2021-11-06T22:00:00.000000000Z
                        \t32.881769076795045\t2021-11-06T23:00:00.000000000Z
                        VTJW\t42.17768841969397\t2021-11-06T23:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-06T23:00:00.000000000Z
                        PEHN\t70.94360487171201\t2021-11-06T23:00:00.000000000Z
                        HYRX\t97.71103146051203\t2021-11-06T23:00:00.000000000Z
                        \t32.881769076795045\t2021-11-07T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t2021-11-07T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-07T00:00:00.000000000Z
                        PEHN\t81.46807944500559\t2021-11-07T00:00:00.000000000Z
                        HYRX\t97.71103146051203\t2021-11-07T00:00:00.000000000Z
                        \t57.93466326862211\t2021-11-07T01:00:00.000000000Z
                        VTJW\t90.9981994382809\t2021-11-07T01:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-07T01:00:00.000000000Z
                        PEHN\t81.46807944500559\t2021-11-07T01:00:00.000000000Z
                        HYRX\t12.026122412833129\t2021-11-07T01:00:00.000000000Z
                        \t26.922103479744898\t2021-11-07T02:00:00.000000000Z
                        VTJW\t48.820511018586934\t2021-11-07T02:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-07T02:00:00.000000000Z
                        PEHN\t81.46807944500559\t2021-11-07T02:00:00.000000000Z
                        HYRX\t12.026122412833129\t2021-11-07T02:00:00.000000000Z
                        \t52.98405941762054\t2021-11-07T03:00:00.000000000Z
                        VTJW\t48.820511018586934\t2021-11-07T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-07T03:00:00.000000000Z
                        PEHN\t81.46807944500559\t2021-11-07T03:00:00.000000000Z
                        HYRX\t12.026122412833129\t2021-11-07T03:00:00.000000000Z
                        \t52.98405941762054\t2021-11-07T04:00:00.000000000Z
                        VTJW\t48.820511018586934\t2021-11-07T04:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-07T04:00:00.000000000Z
                        PEHN\t84.45258177211063\t2021-11-07T04:00:00.000000000Z
                        HYRX\t12.026122412833129\t2021-11-07T04:00:00.000000000Z
                        \t97.5019885372507\t2021-11-07T05:00:00.000000000Z
                        VTJW\t48.820511018586934\t2021-11-07T05:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-07T05:00:00.000000000Z
                        PEHN\t84.45258177211063\t2021-11-07T05:00:00.000000000Z
                        HYRX\t12.026122412833129\t2021-11-07T05:00:00.000000000Z
                        \t80.01121139739173\t2021-11-07T06:00:00.000000000Z
                        VTJW\t48.820511018586934\t2021-11-07T06:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-07T06:00:00.000000000Z
                        PEHN\t49.00510449885239\t2021-11-07T06:00:00.000000000Z
                        HYRX\t12.026122412833129\t2021-11-07T06:00:00.000000000Z
                        \t92.050039469858\t2021-11-07T07:00:00.000000000Z
                        VTJW\t48.820511018586934\t2021-11-07T07:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-07T07:00:00.000000000Z
                        PEHN\t49.00510449885239\t2021-11-07T07:00:00.000000000Z
                        HYRX\t12.026122412833129\t2021-11-07T07:00:00.000000000Z
                        \t45.6344569609078\t2021-11-07T08:00:00.000000000Z
                        VTJW\t48.820511018586934\t2021-11-07T08:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-07T08:00:00.000000000Z
                        PEHN\t49.00510449885239\t2021-11-07T08:00:00.000000000Z
                        HYRX\t12.026122412833129\t2021-11-07T08:00:00.000000000Z
                        \t40.455469747939254\t2021-11-07T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t2021-11-07T09:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-07T09:00:00.000000000Z
                        PEHN\t49.00510449885239\t2021-11-07T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t2021-11-07T09:00:00.000000000Z
                        """,
                "select b, s, to_timezone(k, 'EST') k from (select b, sum(a) s, k from x sample by 1h fill(prev) align to calendar time zone 'EST')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-11-06T22:10:00.000000000Z' as timestamp_ns), 3100000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                null,
                false
        );
    }

    @Test
    public void testSampleFillPrevAllTypes() throws Exception {
        assertQuery(
                """
                        a\tb\tc\td\te\tf\tg\ti\tj\tl\tm\tp\tvch\tsum\tk
                        1569490116\tfalse\tZ\tnull\t0.7611029\t428\t2015-05-16T20:27:48.158Z\tVTJW\t-8671107786057422727\t26\t00000000 68 61 26 af 19 c4 95 94 36 53 49\t1970-01-01T00:00:00.000000000Z\tjFxO]0L#Y\t0.15786635599554755\t1970-01-03T00:00:00.000000000Z
                        -2002373666\ttrue\tU\t0.7883065830055033\t0.76642567\t401\t2015-09-20T21:49:18.129Z\t\t5334238747895433003\t10\t00000000 8e 78 b5 b9 11 53 d0 fb 64 bb 1a d4 f0 2d 40 e2
                        00000010 4b b1 3e\t1970-01-01T01:00:00.000000000Z\tЃَᯤ\\篸{\uD9D7\uDFE5\uDAE9\uDF46OFг\uDBAE\uDD12ɜ|\\軦\t0.09750574414434399\t1970-01-03T00:00:00.000000000Z
                        -283321892\tfalse\t\t0.8438459563914771\t0.13006097\t736\t2015-01-13T04:07:44.289Z\tPEHN\t5398991075259361292\t4\t00000000 63 b7 c2 9f 29 8e 29 5e 69 c6 eb ea c3 c9 73 93
                        00000010 46 fe\t1970-01-01T02:00:00.000000000Z\tG -$}\t0.22631523434159562\t1970-01-03T00:00:00.000000000Z
                        -2108151088\ttrue\tK\t0.5185631921367574\t0.20585066\t598\t2015-02-06T22:58:50.333Z\t\t5552835357100545895\t4\t00000000 b0 ec 0b 92 58 7d 24 bc 2e 60 6a 1c\t1970-01-01T03:00:00.000000000Z\tkiM,1Dzq\tnull\t1970-01-03T00:00:00.000000000Z
                        1826239903\ttrue\tO\t0.06578761277152223\t0.38402128\t291\t2015-08-08T02:35:56.961Z\tVTJW\t-8653777305694768077\t26\t00000000 a3 67 7a 1a 79 e4 35 e4 3a dc 5c 65 ff 27 67\t1970-01-01T04:00:00.000000000Z\t5o\\S1l1S -(\tnull\t1970-01-03T00:00:00.000000000Z
                        1569490116\tfalse\tZ\tnull\t0.7611029\t428\t2015-05-16T20:27:48.158Z\tVTJW\t-8671107786057422727\t26\t00000000 68 61 26 af 19 c4 95 94 36 53 49\t1970-01-01T00:00:00.000000000Z\tjFxO]0L#Y\t0.15786635599554755\t1970-01-03T03:00:00.000000000Z
                        -2002373666\ttrue\tU\t0.7883065830055033\t0.76642567\t401\t2015-09-20T21:49:18.129Z\t\t5334238747895433003\t10\t00000000 8e 78 b5 b9 11 53 d0 fb 64 bb 1a d4 f0 2d 40 e2
                        00000010 4b b1 3e\t1970-01-01T01:00:00.000000000Z\tЃَᯤ\\篸{\uD9D7\uDFE5\uDAE9\uDF46OFг\uDBAE\uDD12ɜ|\\軦\t0.09750574414434399\t1970-01-03T03:00:00.000000000Z
                        -283321892\tfalse\t\t0.8438459563914771\t0.13006097\t736\t2015-01-13T04:07:44.289Z\tPEHN\t5398991075259361292\t4\t00000000 63 b7 c2 9f 29 8e 29 5e 69 c6 eb ea c3 c9 73 93
                        00000010 46 fe\t1970-01-01T02:00:00.000000000Z\tG -$}\t0.22631523434159562\t1970-01-03T03:00:00.000000000Z
                        -2108151088\ttrue\tK\t0.5185631921367574\t0.20585066\t598\t2015-02-06T22:58:50.333Z\t\t5552835357100545895\t4\t00000000 b0 ec 0b 92 58 7d 24 bc 2e 60 6a 1c\t1970-01-01T03:00:00.000000000Z\tkiM,1Dzq\t0.043606408996349044\t1970-01-03T03:00:00.000000000Z
                        1826239903\ttrue\tO\t0.06578761277152223\t0.38402128\t291\t2015-08-08T02:35:56.961Z\tVTJW\t-8653777305694768077\t26\t00000000 a3 67 7a 1a 79 e4 35 e4 3a dc 5c 65 ff 27 67\t1970-01-01T04:00:00.000000000Z\t5o\\S1l1S -(\t0.6810852005509421\t1970-01-03T03:00:00.000000000Z
                        """,
                "select a,b,c,d,e,f,g,i,j,l,m,p,vch,sum(o), k from x sample by 3h fill(prev)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_int() a," +
                        " rnd_boolean() b," +
                        " rnd_str(1,1,2) c," +
                        " rnd_double(2) d," +
                        " rnd_float(2) e," +
                        " rnd_short(10,1024) f," +
                        " rnd_date(to_date('2015', 'yyyy'), to_date('2016', 'yyyy'), 2) g," +
                        " rnd_symbol(4,4,4,2) i," +
                        " rnd_long() j," +
                        " rnd_byte(2,50) l," +
                        " rnd_bin(10, 20, 2) m," +
                        " rnd_str(5,16,2) n," +
                        " rnd_double(2) o," +
                        " rnd_varchar(5,16,2) vch," +
                        " timestamp_sequence_ns(0, 3600000000000) p," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillPrevDuplicateKey() throws Exception {
        assertQuery(
                """
                        b\tb1\tb2\tsum\tk
                        \t\t\t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\tnull\t1970-01-03T00:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\tnull\t1970-01-03T00:00:00.000000000Z
                        \t\t\t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t42.17768841969397\t1970-01-03T03:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T03:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\tnull\t1970-01-03T03:00:00.000000000Z
                        \t\t\t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t42.17768841969397\t1970-01-03T06:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T06:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z
                        \t\t\t26.922103479744898\t1970-01-03T09:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T09:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t81.46807944500559\t1970-01-03T09:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        \t\t\t150.48604795487125\t1970-01-03T12:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t48.820511018586934\t1970-01-03T12:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T12:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t12.026122412833129\t1970-01-03T12:00:00.000000000Z
                        \t\t\t172.06125086724973\t1970-01-03T15:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t48.820511018586934\t1970-01-03T15:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T15:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t12.026122412833129\t1970-01-03T15:00:00.000000000Z
                        \t\t\t86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t48.820511018586934\t1970-01-03T18:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T18:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t49.00510449885239\t1970-01-03T18:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t12.026122412833129\t1970-01-03T18:00:00.000000000Z
                        """,
                "select b, b, b, sum(a), k from x sample by 3h fill(prev)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tb1\tb2\tsum\tk
                        \t\t\t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\tnull\t1970-01-03T00:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\tnull\t1970-01-03T00:00:00.000000000Z
                        UVSD\tUVSD\tUVSD\tnull\t1970-01-03T00:00:00.000000000Z
                        KGHV\tKGHV\tKGHV\tnull\t1970-01-03T00:00:00.000000000Z
                        \t\t\t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t42.17768841969397\t1970-01-03T03:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T03:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\tnull\t1970-01-03T03:00:00.000000000Z
                        UVSD\tUVSD\tUVSD\tnull\t1970-01-03T03:00:00.000000000Z
                        KGHV\tKGHV\tKGHV\tnull\t1970-01-03T03:00:00.000000000Z
                        \t\t\t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t42.17768841969397\t1970-01-03T06:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T06:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z
                        UVSD\tUVSD\tUVSD\tnull\t1970-01-03T06:00:00.000000000Z
                        KGHV\tKGHV\tKGHV\tnull\t1970-01-03T06:00:00.000000000Z
                        \t\t\t26.922103479744898\t1970-01-03T09:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T09:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t81.46807944500559\t1970-01-03T09:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        UVSD\tUVSD\tUVSD\tnull\t1970-01-03T09:00:00.000000000Z
                        KGHV\tKGHV\tKGHV\tnull\t1970-01-03T09:00:00.000000000Z
                        \t\t\t150.48604795487125\t1970-01-03T12:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t48.820511018586934\t1970-01-03T12:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T12:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t12.026122412833129\t1970-01-03T12:00:00.000000000Z
                        UVSD\tUVSD\tUVSD\tnull\t1970-01-03T12:00:00.000000000Z
                        KGHV\tKGHV\tKGHV\tnull\t1970-01-03T12:00:00.000000000Z
                        \t\t\t172.06125086724973\t1970-01-03T15:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t48.820511018586934\t1970-01-03T15:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T15:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t12.026122412833129\t1970-01-03T15:00:00.000000000Z
                        UVSD\tUVSD\tUVSD\tnull\t1970-01-03T15:00:00.000000000Z
                        KGHV\tKGHV\tKGHV\tnull\t1970-01-03T15:00:00.000000000Z
                        \t\t\t86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t48.820511018586934\t1970-01-03T18:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T18:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t49.00510449885239\t1970-01-03T18:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t12.026122412833129\t1970-01-03T18:00:00.000000000Z
                        UVSD\tUVSD\tUVSD\tnull\t1970-01-03T18:00:00.000000000Z
                        KGHV\tKGHV\tKGHV\tnull\t1970-01-03T18:00:00.000000000Z
                        \t\t\t86.08992670884706\t1970-01-03T21:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t48.820511018586934\t1970-01-03T21:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-03T21:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t49.00510449885239\t1970-01-03T21:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t12.026122412833129\t1970-01-03T21:00:00.000000000Z
                        UVSD\tUVSD\tUVSD\tnull\t1970-01-03T21:00:00.000000000Z
                        KGHV\tKGHV\tKGHV\tnull\t1970-01-03T21:00:00.000000000Z
                        \t\t\t86.08992670884706\t1970-01-04T00:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t48.820511018586934\t1970-01-04T00:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-04T00:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t49.00510449885239\t1970-01-04T00:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t12.026122412833129\t1970-01-04T00:00:00.000000000Z
                        UVSD\tUVSD\tUVSD\tnull\t1970-01-04T00:00:00.000000000Z
                        KGHV\tKGHV\tKGHV\tnull\t1970-01-04T00:00:00.000000000Z
                        \t\t\t54.49155021518948\t1970-01-04T03:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t48.820511018586934\t1970-01-04T03:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-04T03:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t49.00510449885239\t1970-01-04T03:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t12.026122412833129\t1970-01-04T03:00:00.000000000Z
                        UVSD\tUVSD\tUVSD\tnull\t1970-01-04T03:00:00.000000000Z
                        KGHV\tKGHV\tKGHV\tnull\t1970-01-04T03:00:00.000000000Z
                        \t\t\t135.835983782176\t1970-01-04T06:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t48.820511018586934\t1970-01-04T06:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-04T06:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t49.00510449885239\t1970-01-04T06:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t12.026122412833129\t1970-01-04T06:00:00.000000000Z
                        UVSD\tUVSD\tUVSD\t49.42890511958454\t1970-01-04T06:00:00.000000000Z
                        KGHV\tKGHV\tKGHV\tnull\t1970-01-04T06:00:00.000000000Z
                        \t\t\t135.835983782176\t1970-01-04T09:00:00.000000000Z
                        VTJW\tVTJW\tVTJW\t48.820511018586934\t1970-01-04T09:00:00.000000000Z
                        RXGZ\tRXGZ\tRXGZ\t23.90529010846525\t1970-01-04T09:00:00.000000000Z
                        PEHN\tPEHN\tPEHN\t49.00510449885239\t1970-01-04T09:00:00.000000000Z
                        HYRX\tHYRX\tHYRX\t12.026122412833129\t1970-01-04T09:00:00.000000000Z
                        UVSD\tUVSD\tUVSD\t49.42890511958454\t1970-01-04T09:00:00.000000000Z
                        KGHV\tKGHV\tKGHV\t67.52509547112409\t1970-01-04T09:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillPrevDuplicateTimestamp1() throws Exception {
        assertQuery(
                """
                        b\tsum\tk\tk1
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        PEHN\tnull\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        """,
                "select b, sum(a), k, k from x sample by 3h fill(prev)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum\tk\tk1
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        PEHN\tnull\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        \t86.08992670884706\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        \t54.49155021518948\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        \t135.835983782176\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        \t135.835983782176\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        KGHV\t67.52509547112409\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillPrevDuplicateTimestamp2() throws Exception {
        assertQuery(
                """
                        b\tsum\tk1\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        PEHN\tnull\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        """,
                "select b, sum(a), k k1, k from x sample by 3h fill(prev)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k1",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum\tk1\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        PEHN\tnull\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        HYRX\tnull\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T06:00:00.000000000Z\t1970-01-03T06:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T09:00:00.000000000Z\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T12:00:00.000000000Z\t1970-01-03T12:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T15:00:00.000000000Z\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T18:00:00.000000000Z\t1970-01-03T18:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        UVSD\tnull\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        KGHV\tnull\t1970-01-03T21:00:00.000000000Z\t1970-01-03T21:00:00.000000000Z
                        \t86.08992670884706\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T00:00:00.000000000Z\t1970-01-04T00:00:00.000000000Z
                        \t54.49155021518948\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        UVSD\tnull\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T03:00:00.000000000Z\t1970-01-04T03:00:00.000000000Z
                        \t135.835983782176\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        KGHV\tnull\t1970-01-04T06:00:00.000000000Z\t1970-01-04T06:00:00.000000000Z
                        \t135.835983782176\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        KGHV\t67.52509547112409\t1970-01-04T09:00:00.000000000Z\t1970-01-04T09:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillPrevEmptyBase() throws Exception {
        assertQuery(
                (CharSequence) null,
                "select a,b,c,d,e,f,g,i,j,l,m,p,sum(o), k from x where 0!=0 sample by 3h fill(prev)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_int() a," +
                        " rnd_boolean() b," +
                        " rnd_str(1,1,2) c," +
                        " rnd_double(2) d," +
                        " rnd_float(2) e," +
                        " rnd_short(10,1024) f," +
                        " rnd_date(to_date('2015', 'yyyy'), to_date('2016', 'yyyy'), 2) g," +
                        " rnd_symbol(4,4,4,2) i," +
                        " rnd_long() j," +
                        " rnd_byte(2,50) l," +
                        " rnd_bin(10, 20, 2) m," +
                        " rnd_str(5,16,2) n," +
                        " rnd_double(2) o," +
                        " timestamp_sequence_ns(0, 3600000000000) p," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillPrevNoTimestamp() throws Exception {
        assertQuery(
                """
                        b\tsum
                        \t11.427984775756228
                        VTJW\t42.17768841969397
                        RXGZ\t23.90529010846525
                        PEHN\tnull
                        HYRX\tnull
                        \t120.87811633071126
                        VTJW\t42.17768841969397
                        RXGZ\t23.90529010846525
                        PEHN\t70.94360487171201
                        HYRX\tnull
                        \t57.93466326862211
                        VTJW\t42.17768841969397
                        RXGZ\t23.90529010846525
                        PEHN\t81.46807944500559
                        HYRX\t97.71103146051203
                        \t26.922103479744898
                        VTJW\t48.820511018586934
                        RXGZ\t23.90529010846525
                        PEHN\t81.46807944500559
                        HYRX\t12.026122412833129
                        \t150.48604795487125
                        VTJW\t48.820511018586934
                        RXGZ\t23.90529010846525
                        PEHN\t84.45258177211063
                        HYRX\t12.026122412833129
                        \t172.06125086724973
                        VTJW\t48.820511018586934
                        RXGZ\t23.90529010846525
                        PEHN\t49.00510449885239
                        HYRX\t12.026122412833129
                        \t86.08992670884706
                        VTJW\t48.820511018586934
                        RXGZ\t23.90529010846525
                        PEHN\t49.00510449885239
                        HYRX\t12.026122412833129
                        """,
                "select b, sum(a) from x sample by 3h fill(prev)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(172800000000, 3600000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                null,
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(277200000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum
                        \t11.427984775756228
                        VTJW\t42.17768841969397
                        RXGZ\t23.90529010846525
                        PEHN\tnull
                        HYRX\tnull
                        UVSD\tnull
                        KGHV\tnull
                        \t120.87811633071126
                        VTJW\t42.17768841969397
                        RXGZ\t23.90529010846525
                        PEHN\t70.94360487171201
                        HYRX\tnull
                        UVSD\tnull
                        KGHV\tnull
                        \t57.93466326862211
                        VTJW\t42.17768841969397
                        RXGZ\t23.90529010846525
                        PEHN\t81.46807944500559
                        HYRX\t97.71103146051203
                        UVSD\tnull
                        KGHV\tnull
                        \t26.922103479744898
                        VTJW\t48.820511018586934
                        RXGZ\t23.90529010846525
                        PEHN\t81.46807944500559
                        HYRX\t12.026122412833129
                        UVSD\tnull
                        KGHV\tnull
                        \t150.48604795487125
                        VTJW\t48.820511018586934
                        RXGZ\t23.90529010846525
                        PEHN\t84.45258177211063
                        HYRX\t12.026122412833129
                        UVSD\tnull
                        KGHV\tnull
                        \t172.06125086724973
                        VTJW\t48.820511018586934
                        RXGZ\t23.90529010846525
                        PEHN\t49.00510449885239
                        HYRX\t12.026122412833129
                        UVSD\tnull
                        KGHV\tnull
                        \t86.08992670884706
                        VTJW\t48.820511018586934
                        RXGZ\t23.90529010846525
                        PEHN\t49.00510449885239
                        HYRX\t12.026122412833129
                        UVSD\tnull
                        KGHV\tnull
                        \t86.08992670884706
                        VTJW\t48.820511018586934
                        RXGZ\t23.90529010846525
                        PEHN\t49.00510449885239
                        HYRX\t12.026122412833129
                        UVSD\tnull
                        KGHV\tnull
                        \t86.08992670884706
                        VTJW\t48.820511018586934
                        RXGZ\t23.90529010846525
                        PEHN\t49.00510449885239
                        HYRX\t12.026122412833129
                        UVSD\tnull
                        KGHV\tnull
                        \t54.49155021518948
                        VTJW\t48.820511018586934
                        RXGZ\t23.90529010846525
                        PEHN\t49.00510449885239
                        HYRX\t12.026122412833129
                        UVSD\tnull
                        KGHV\tnull
                        \t135.835983782176
                        VTJW\t48.820511018586934
                        RXGZ\t23.90529010846525
                        PEHN\t49.00510449885239
                        HYRX\t12.026122412833129
                        UVSD\t49.42890511958454
                        KGHV\tnull
                        \t135.835983782176
                        VTJW\t48.820511018586934
                        RXGZ\t23.90529010846525
                        PEHN\t49.00510449885239
                        HYRX\t12.026122412833129
                        UVSD\t49.42890511958454
                        KGHV\t67.52509547112409
                        """,
                false
        );
    }

    @Test
    public void testSampleFillPrevNoTimestampLong256AndChar() throws Exception {
        assertQuery(
                """
                        a\tb\tsum
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\tnull
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\tnull
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\tnull
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\tnull
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\tnull
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\tnull
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\tnull
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\tnull
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\tnull
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\tnull
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\tnull
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\tnull
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\tnull
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\tnull
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\tnull
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\tnull
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\tnull
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\tnull
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\tnull
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\tnull
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\tnull
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\tnull
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\tnull
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\tnull
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\tnull
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\tnull
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\tnull
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\tnull
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\tnull
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\tnull
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\tnull
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\tnull
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\tnull
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\tnull
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\tnull
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\tnull
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\tnull
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\tnull
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\tnull
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\t0.3435685332942956
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\t0.4138164748227684
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\t0.7763904674818695
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\tnull
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\tnull
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\tnull
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\tnull
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\tnull
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\tnull
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\tnull
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\t0.3435685332942956
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\t0.4138164748227684
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\t0.7763904674818695
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\t0.4900510449885239
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\t0.38642336707855873
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\t0.6590341607692226
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\tnull
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\tnull
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\tnull
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\tnull
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\t0.3435685332942956
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\t0.4138164748227684
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\t0.7763904674818695
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\t0.4900510449885239
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\t0.38642336707855873
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\t0.6590341607692226
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\t0.5659429139861241
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\t0.45659895188239796
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\t0.5778947915182423
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\tnull
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\t0.3435685332942956
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\t0.4138164748227684
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\t0.7763904674818695
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\t0.4900510449885239
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\t0.38642336707855873
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\t0.6590341607692226
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\t0.5659429139861241
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\t0.45659895188239796
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\t0.5778947915182423
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\t0.325403220015421
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\t0.49428905119584543
                        """,
                "select a, b, sum(c) from x sample by 3h fill(prev)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_long256() a," +
                        " rnd_char() b," +
                        " rnd_double() c, " +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                null,
                "insert into x select * from (" +
                        "select" +
                        " rnd_long256() a," +
                        " rnd_char() b," +
                        " rnd_double() c, " +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        a\tb\tsum
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\tnull
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\tnull
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\tnull
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\tnull
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\tnull
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\tnull
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\tnull
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\tnull
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\tnull
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\tnull
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\tnull
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\tnull
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\tnull
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\tnull
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\tnull
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\tnull
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\tnull
                        0xaa1896d0ad3419d2910aa7b6d58506dc7c97a2cb4ac4b04722556b928447b584\tD\tnull
                        0x0cb5f439cbc22e9d1f0481ab7acd1f4a77827c4f6b03027bc6dfacdd3f3c52b8\tO\tnull
                        0xacb025f759cffbd0de9be4e331fe36e67dc859770af204938151081b8acafadd\tB\tnull
                        0x9d6cb7b4fbf1fa48dbd7587f207765769b4bae41862e09ccb482cff57e9c5398\tK\tnull
                        0xaf44c40a67ef5e1c5b3ef21223ee884965009e89eacf0aadd25adf928386cdd2\tQ\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\tnull
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\tnull
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\tnull
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\tnull
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\tnull
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\tnull
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\tnull
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\tnull
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\tnull
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\tnull
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\tnull
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\tnull
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\tnull
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\tnull
                        0xaa1896d0ad3419d2910aa7b6d58506dc7c97a2cb4ac4b04722556b928447b584\tD\tnull
                        0x0cb5f439cbc22e9d1f0481ab7acd1f4a77827c4f6b03027bc6dfacdd3f3c52b8\tO\tnull
                        0xacb025f759cffbd0de9be4e331fe36e67dc859770af204938151081b8acafadd\tB\tnull
                        0x9d6cb7b4fbf1fa48dbd7587f207765769b4bae41862e09ccb482cff57e9c5398\tK\tnull
                        0xaf44c40a67ef5e1c5b3ef21223ee884965009e89eacf0aadd25adf928386cdd2\tQ\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\tnull
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\tnull
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\tnull
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\tnull
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\tnull
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\tnull
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\tnull
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\tnull
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\tnull
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\tnull
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\tnull
                        0xaa1896d0ad3419d2910aa7b6d58506dc7c97a2cb4ac4b04722556b928447b584\tD\tnull
                        0x0cb5f439cbc22e9d1f0481ab7acd1f4a77827c4f6b03027bc6dfacdd3f3c52b8\tO\tnull
                        0xacb025f759cffbd0de9be4e331fe36e67dc859770af204938151081b8acafadd\tB\tnull
                        0x9d6cb7b4fbf1fa48dbd7587f207765769b4bae41862e09ccb482cff57e9c5398\tK\tnull
                        0xaf44c40a67ef5e1c5b3ef21223ee884965009e89eacf0aadd25adf928386cdd2\tQ\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\t0.3435685332942956
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\t0.4138164748227684
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\t0.7763904674818695
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\tnull
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\tnull
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\tnull
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\tnull
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\tnull
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\tnull
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\tnull
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\tnull
                        0xaa1896d0ad3419d2910aa7b6d58506dc7c97a2cb4ac4b04722556b928447b584\tD\tnull
                        0x0cb5f439cbc22e9d1f0481ab7acd1f4a77827c4f6b03027bc6dfacdd3f3c52b8\tO\tnull
                        0xacb025f759cffbd0de9be4e331fe36e67dc859770af204938151081b8acafadd\tB\tnull
                        0x9d6cb7b4fbf1fa48dbd7587f207765769b4bae41862e09ccb482cff57e9c5398\tK\tnull
                        0xaf44c40a67ef5e1c5b3ef21223ee884965009e89eacf0aadd25adf928386cdd2\tQ\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\t0.3435685332942956
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\t0.4138164748227684
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\t0.7763904674818695
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\t0.4900510449885239
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\t0.38642336707855873
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\t0.6590341607692226
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\tnull
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\tnull
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\tnull
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\tnull
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\tnull
                        0xaa1896d0ad3419d2910aa7b6d58506dc7c97a2cb4ac4b04722556b928447b584\tD\tnull
                        0x0cb5f439cbc22e9d1f0481ab7acd1f4a77827c4f6b03027bc6dfacdd3f3c52b8\tO\tnull
                        0xacb025f759cffbd0de9be4e331fe36e67dc859770af204938151081b8acafadd\tB\tnull
                        0x9d6cb7b4fbf1fa48dbd7587f207765769b4bae41862e09ccb482cff57e9c5398\tK\tnull
                        0xaf44c40a67ef5e1c5b3ef21223ee884965009e89eacf0aadd25adf928386cdd2\tQ\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\t0.3435685332942956
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\t0.4138164748227684
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\t0.7763904674818695
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\t0.4900510449885239
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\t0.38642336707855873
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\t0.6590341607692226
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\t0.5659429139861241
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\t0.45659895188239796
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\t0.5778947915182423
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\tnull
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\tnull
                        0xaa1896d0ad3419d2910aa7b6d58506dc7c97a2cb4ac4b04722556b928447b584\tD\tnull
                        0x0cb5f439cbc22e9d1f0481ab7acd1f4a77827c4f6b03027bc6dfacdd3f3c52b8\tO\tnull
                        0xacb025f759cffbd0de9be4e331fe36e67dc859770af204938151081b8acafadd\tB\tnull
                        0x9d6cb7b4fbf1fa48dbd7587f207765769b4bae41862e09ccb482cff57e9c5398\tK\tnull
                        0xaf44c40a67ef5e1c5b3ef21223ee884965009e89eacf0aadd25adf928386cdd2\tQ\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\t0.3435685332942956
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\t0.4138164748227684
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\t0.7763904674818695
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\t0.4900510449885239
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\t0.38642336707855873
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\t0.6590341607692226
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\t0.5659429139861241
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\t0.45659895188239796
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\t0.5778947915182423
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\t0.325403220015421
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\t0.49428905119584543
                        0xaa1896d0ad3419d2910aa7b6d58506dc7c97a2cb4ac4b04722556b928447b584\tD\tnull
                        0x0cb5f439cbc22e9d1f0481ab7acd1f4a77827c4f6b03027bc6dfacdd3f3c52b8\tO\tnull
                        0xacb025f759cffbd0de9be4e331fe36e67dc859770af204938151081b8acafadd\tB\tnull
                        0x9d6cb7b4fbf1fa48dbd7587f207765769b4bae41862e09ccb482cff57e9c5398\tK\tnull
                        0xaf44c40a67ef5e1c5b3ef21223ee884965009e89eacf0aadd25adf928386cdd2\tQ\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\t0.3435685332942956
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\t0.4138164748227684
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\t0.7763904674818695
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\t0.4900510449885239
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\t0.38642336707855873
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\t0.6590341607692226
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\t0.5659429139861241
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\t0.45659895188239796
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\t0.5778947915182423
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\t0.325403220015421
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\t0.49428905119584543
                        0xaa1896d0ad3419d2910aa7b6d58506dc7c97a2cb4ac4b04722556b928447b584\tD\tnull
                        0x0cb5f439cbc22e9d1f0481ab7acd1f4a77827c4f6b03027bc6dfacdd3f3c52b8\tO\tnull
                        0xacb025f759cffbd0de9be4e331fe36e67dc859770af204938151081b8acafadd\tB\tnull
                        0x9d6cb7b4fbf1fa48dbd7587f207765769b4bae41862e09ccb482cff57e9c5398\tK\tnull
                        0xaf44c40a67ef5e1c5b3ef21223ee884965009e89eacf0aadd25adf928386cdd2\tQ\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\t0.3435685332942956
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\t0.4138164748227684
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\t0.7763904674818695
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\t0.4900510449885239
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\t0.38642336707855873
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\t0.6590341607692226
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\t0.5659429139861241
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\t0.45659895188239796
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\t0.5778947915182423
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\t0.325403220015421
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\t0.49428905119584543
                        0xaa1896d0ad3419d2910aa7b6d58506dc7c97a2cb4ac4b04722556b928447b584\tD\tnull
                        0x0cb5f439cbc22e9d1f0481ab7acd1f4a77827c4f6b03027bc6dfacdd3f3c52b8\tO\tnull
                        0xacb025f759cffbd0de9be4e331fe36e67dc859770af204938151081b8acafadd\tB\tnull
                        0x9d6cb7b4fbf1fa48dbd7587f207765769b4bae41862e09ccb482cff57e9c5398\tK\tnull
                        0xaf44c40a67ef5e1c5b3ef21223ee884965009e89eacf0aadd25adf928386cdd2\tQ\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\t0.3435685332942956
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\t0.4138164748227684
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\t0.7763904674818695
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\t0.4900510449885239
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\t0.38642336707855873
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\t0.6590341607692226
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\t0.5659429139861241
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\t0.45659895188239796
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\t0.5778947915182423
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\t0.325403220015421
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\t0.49428905119584543
                        0xaa1896d0ad3419d2910aa7b6d58506dc7c97a2cb4ac4b04722556b928447b584\tD\t0.4971342426836798
                        0x0cb5f439cbc22e9d1f0481ab7acd1f4a77827c4f6b03027bc6dfacdd3f3c52b8\tO\tnull
                        0xacb025f759cffbd0de9be4e331fe36e67dc859770af204938151081b8acafadd\tB\tnull
                        0x9d6cb7b4fbf1fa48dbd7587f207765769b4bae41862e09ccb482cff57e9c5398\tK\tnull
                        0xaf44c40a67ef5e1c5b3ef21223ee884965009e89eacf0aadd25adf928386cdd2\tQ\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\t0.3435685332942956
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\t0.4138164748227684
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\t0.7763904674818695
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\t0.4900510449885239
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\t0.38642336707855873
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\t0.6590341607692226
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\t0.5659429139861241
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\t0.45659895188239796
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\t0.5778947915182423
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\t0.325403220015421
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\t0.49428905119584543
                        0xaa1896d0ad3419d2910aa7b6d58506dc7c97a2cb4ac4b04722556b928447b584\tD\t0.4971342426836798
                        0x0cb5f439cbc22e9d1f0481ab7acd1f4a77827c4f6b03027bc6dfacdd3f3c52b8\tO\t0.44804689668613573
                        0xacb025f759cffbd0de9be4e331fe36e67dc859770af204938151081b8acafadd\tB\t0.2879973939681931
                        0x9d6cb7b4fbf1fa48dbd7587f207765769b4bae41862e09ccb482cff57e9c5398\tK\t0.24008362859107102
                        0xaf44c40a67ef5e1c5b3ef21223ee884965009e89eacf0aadd25adf928386cdd2\tQ\tnull
                        0x9f9b2131d49fcd1d6b8139815c50d3410010cde812ce60ee0010a928bb8b9650\tC\t0.2845577791213847
                        0x797fa69eb8fec6cce8beef38cd7bb3d8db2d34586f6275fab5b2159a23565217\tX\t0.8423410920883345
                        0x716de3d25dcc2d919fa2397a5d8c84c4c1e631285c1ab288c72bfc5230158059\tG\t0.3491070363730514
                        0xc8b1863d4316f9c773b27651a916ab1b568bc2d7a4aa860483881d4171847cf3\tB\t0.5599161804800813
                        0x61b1a0b0a559551538b73d329210d2774cdfb9e29522133c87aa0968faec6879\tF\t0.6693837147631712
                        0x74ce62a98a4516952705e02c613acfc405374f5fbcef4819523eb59d99c647af\tY\t0.8799634725391621
                        0x7f98b0c74238337e36ee542d654d22598a538661f350d0b46f06560981acb549\tO\t0.021651819007252326
                        0xcec82869edec121bc2593f82b430328d84a09f29df637e3863eb3740c80f661e\tS\t0.6381607531178513
                        0x6afe61bd7c4ae0d84c0094500fbffdfe76fb2001fe5dfb09acea66fbe47c5e39\tS\t0.9687423276940171
                        0x94cfe42988a633de738bab883dc7e3323239ad1b0411a66a10bb226eb4243e36\tQ\t0.3435685332942956
                        0x2bbfcf66bab932fc5ea744ebab75d542a937c9ce75e81607a1b56c3d802c4735\tG\t0.4138164748227684
                        0x98c2d832d83de9934a0705e1136e872b3ad08d6037d3ce8155c06051ee52138b\tS\t0.7763904674818695
                        0x60802a2ca499f211b771e27f939096b9c356f99ae70523b585b80cec619f9178\tT\t0.4900510449885239
                        0x6a0accd425e948d49a77e857727e751a7d67d36a09a1b5bb2932c3ad61000d64\tZ\t0.38642336707855873
                        0x687a84abb7bfac3ebedf29efb28cdcb1b75dccbdf1f8b84b9b27eba5e9cfa1e2\tI\t0.6590341607692226
                        0x30d46a3a4749c41d7a902c77fa1a889c51686790e59377ca68653a6cd896f81e\tI\t0.5659429139861241
                        0x997918f622d62989c009aea26fdde482ba37e200ad5b17cdada00dc8b85c1bc8\tV\t0.45659895188239796
                        0x7d4f1da8fd48b2c3d364c241dde2cf90a7a8f4e549997e46516e1efd8bbcecf6\tS\t0.5778947915182423
                        0xbbdfe8ff0cd60c64712fde5706d6ea2f545ded49c47eea6162d6d100c964eee5\tG\t0.325403220015421
                        0x7ebaf6ca993f8fc98b1309cf32d68bb8aa7dc4eccb68146fb37f1ec82752c7d7\tC\t0.49428905119584543
                        0xaa1896d0ad3419d2910aa7b6d58506dc7c97a2cb4ac4b04722556b928447b584\tD\t0.4971342426836798
                        0x0cb5f439cbc22e9d1f0481ab7acd1f4a77827c4f6b03027bc6dfacdd3f3c52b8\tO\t0.44804689668613573
                        0xacb025f759cffbd0de9be4e331fe36e67dc859770af204938151081b8acafadd\tB\t0.2879973939681931
                        0x9d6cb7b4fbf1fa48dbd7587f207765769b4bae41862e09ccb482cff57e9c5398\tK\t0.24008362859107102
                        0xaf44c40a67ef5e1c5b3ef21223ee884965009e89eacf0aadd25adf928386cdd2\tQ\t0.7446000371089992
                        """,
                false
        );
    }

    @Test
    public void testSampleFillPrevNotKeyed() throws Exception {
        assertQuery(
                """
                        sum\tk
                        0.8745454354091133\t1970-01-01T00:00:17.280000000Z
                        1.551810133791102\t1970-01-01T03:00:17.280000000Z
                        0.8214274286283418\t1970-01-01T06:00:17.280000000Z
                        1.2509938088155907\t1970-01-01T09:00:17.280000000Z
                        1.374822334421568\t1970-01-01T12:00:17.280000000Z
                        1.2326807412877587\t1970-01-01T15:00:17.280000000Z
                        2.1719710889714183\t1970-01-01T18:00:17.280000000Z
                        1.4447447064927308\t1970-01-01T21:00:17.280000000Z
                        1.6328006113717726\t1970-01-02T00:00:17.280000000Z
                        1.2084207597347858\t1970-01-02T03:00:17.280000000Z
                        2.3834635376399724\t1970-01-02T06:00:17.280000000Z
                        1.6181165075977018\t1970-01-02T09:00:17.280000000Z
                        1.618269955964484\t1970-01-02T12:00:17.280000000Z
                        2.090927105391142\t1970-01-02T15:00:17.280000000Z
                        1.2480423712293227\t1970-01-02T18:00:17.280000000Z
                        2.587279129812145\t1970-01-02T21:00:17.280000000Z
                        1.467047661180466\t1970-01-03T00:00:17.280000000Z
                        0.629161709851853\t1970-01-03T03:00:17.280000000Z
                        2.191264288796364\t1970-01-03T06:00:17.280000000Z
                        1.3805554422849617\t1970-01-03T09:00:17.280000000Z
                        1.8906117848689568\t1970-01-03T12:00:17.280000000Z
                        1.914394848761218\t1970-01-03T15:00:17.280000000Z
                        1.7077466009740325\t1970-01-03T18:00:17.280000000Z
                        2.9091868315808678\t1970-01-03T21:00:17.280000000Z
                        0.34488282893630495\t1970-01-04T00:00:17.280000000Z
                        1.7359881138274678\t1970-01-04T03:00:17.280000000Z
                        1.1047508985515524\t1970-01-04T06:00:17.280000000Z
                        0.9365397496939732\t1970-01-04T09:00:17.280000000Z
                        1.676203094841128\t1970-01-04T12:00:17.280000000Z
                        1.28493295522627\t1970-01-04T15:00:17.280000000Z
                        1.23855454246846\t1970-01-04T18:00:17.280000000Z
                        1.3334813459559705\t1970-01-04T21:00:17.280000000Z
                        0.8049508417119063\t1970-01-05T00:00:17.280000000Z
                        0.9618013985447664\t1970-01-05T03:00:17.280000000Z
                        """,
                "select sum(o), k from x sample by 3h fill(prev) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_int() a," +
                        " rnd_boolean() b," +
                        " rnd_str(1,1,2) c," +
                        " rnd_double(2) d," +
                        " rnd_float(2) e," +
                        " rnd_short(10,1024) f," +
                        " rnd_date(to_date('2015', 'yyyy'), to_date('2016', 'yyyy'), 2) g," +
                        " rnd_symbol(4,4,4,2) i," +
                        " rnd_long() j," +
                        " rnd_byte(2,50) l," +
                        " rnd_bin(10, 20, 2) m," +
                        " rnd_str(5,16,2) n," +
                        " rnd_double(2) o," +
                        " timestamp_sequence_ns(0, 3600000000000) p," +
                        " timestamp_sequence_ns(17280000000, 3000000000000) k" +
                        " from" +
                        " long_sequence(120)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillPrevNotKeyedAlignToCalendar() throws Exception {
        assertQuery(
                """
                        sum\tk
                        0.15786635599554755\t2021-10-31T00:00:00.000000000Z
                        0.04142812470232493\t2021-10-31T00:30:00.000000000Z
                        0.04142812470232493\t2021-10-31T01:00:00.000000000Z
                        0.6752509547112409\t2021-10-31T01:30:00.000000000Z
                        0.6752509547112409\t2021-10-31T02:00:00.000000000Z
                        null\t2021-10-31T02:30:00.000000000Z
                        null\t2021-10-31T03:00:00.000000000Z
                        0.22631523434159562\t2021-10-31T03:30:00.000000000Z
                        0.22631523434159562\t2021-10-31T04:00:00.000000000Z
                        0.6940904779678791\t2021-10-31T04:30:00.000000000Z
                        0.6940904779678791\t2021-10-31T05:00:00.000000000Z
                        0.5913874468544745\t2021-10-31T05:30:00.000000000Z
                        0.5913874468544745\t2021-10-31T06:00:00.000000000Z
                        0.04001697462715281\t2021-10-31T06:30:00.000000000Z
                        0.04001697462715281\t2021-10-31T07:00:00.000000000Z
                        0.07828020681514525\t2021-10-31T07:30:00.000000000Z
                        0.07828020681514525\t2021-10-31T08:00:00.000000000Z
                        null\t2021-10-31T08:30:00.000000000Z
                        0.7431472218131966\t2021-10-31T09:00:00.000000000Z
                        0.7431472218131966\t2021-10-31T09:30:00.000000000Z
                        0.13312214396754163\t2021-10-31T10:00:00.000000000Z
                        0.13312214396754163\t2021-10-31T10:30:00.000000000Z
                        null\t2021-10-31T11:00:00.000000000Z
                        null\t2021-10-31T11:30:00.000000000Z
                        0.2325041018786207\t2021-10-31T12:00:00.000000000Z
                        0.2325041018786207\t2021-10-31T12:30:00.000000000Z
                        0.8853675629694284\t2021-10-31T13:00:00.000000000Z
                        0.8853675629694284\t2021-10-31T13:30:00.000000000Z
                        0.6940917925148332\t2021-10-31T14:00:00.000000000Z
                        0.6940917925148332\t2021-10-31T14:30:00.000000000Z
                        0.4031733414086601\t2021-10-31T15:00:00.000000000Z
                        0.4031733414086601\t2021-10-31T15:30:00.000000000Z
                        0.27755720049807464\t2021-10-31T16:00:00.000000000Z
                        0.27755720049807464\t2021-10-31T16:30:00.000000000Z
                        0.6361737673041902\t2021-10-31T17:00:00.000000000Z
                        0.5965069739835686\t2021-10-31T17:30:00.000000000Z
                        0.5965069739835686\t2021-10-31T18:00:00.000000000Z
                        null\t2021-10-31T18:30:00.000000000Z
                        null\t2021-10-31T19:00:00.000000000Z
                        null\t2021-10-31T19:30:00.000000000Z
                        null\t2021-10-31T20:00:00.000000000Z
                        0.5785645380474713\t2021-10-31T20:30:00.000000000Z
                        0.5785645380474713\t2021-10-31T21:00:00.000000000Z
                        0.7291265477629812\t2021-10-31T21:30:00.000000000Z
                        0.7291265477629812\t2021-10-31T22:00:00.000000000Z
                        0.8642800031609658\t2021-10-31T22:30:00.000000000Z
                        0.8642800031609658\t2021-10-31T23:00:00.000000000Z
                        null\t2021-10-31T23:30:00.000000000Z
                        null\t2021-11-01T00:00:00.000000000Z
                        0.8925004728084927\t2021-11-01T00:30:00.000000000Z
                        0.8925004728084927\t2021-11-01T01:00:00.000000000Z
                        0.5522442336842381\t2021-11-01T01:30:00.000000000Z
                        null\t2021-11-01T02:00:00.000000000Z
                        null\t2021-11-01T02:30:00.000000000Z
                        0.7504512900310369\t2021-11-01T03:00:00.000000000Z
                        """,
                "select sum(o), k from x sample by 30m fill(prev) align to calendar time zone '+00:30'",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_int() a," +
                        " rnd_boolean() b," +
                        " rnd_str(1,1,2) c," +
                        " rnd_double(2) d," +
                        " rnd_float(2) e," +
                        " rnd_short(10,1024) f," +
                        " rnd_date(to_date('2015', 'yyyy'), to_date('2016', 'yyyy'), 2) g," +
                        " rnd_symbol(4,4,4,2) i," +
                        " rnd_long() j," +
                        " rnd_byte(2,50) l," +
                        " rnd_bin(10, 20, 2) m," +
                        " rnd_str(5,16,2) n," +
                        " rnd_double(2) o," +
                        " timestamp_sequence_ns(cast('2020-03-28T03:20:00.000000000Z' as timestamp_ns), 3600000000000) p," +
                        " timestamp_sequence_ns(cast('2021-10-31T00:00:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(30)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillPrevNotKeyedAlignToCalendarTimeZone() throws Exception {
        // this test verifies transition from Summer to Winter time and
        // clock going backwards. An hour of time should drop out of the result set
        // without the logic trying to back fill things
        assertQuery(
                """
                        s\tto_timezone
                        11.427984775756228\t2021-10-31T03:00:00.000000000Z
                        66.08297852815922\t2021-10-31T03:30:00.000000000Z
                        70.94360487171201\t2021-10-31T04:30:00.000000000Z
                        70.94360487171201\t2021-10-31T05:00:00.000000000Z
                        87.99634725391621\t2021-10-31T05:30:00.000000000Z
                        87.99634725391621\t2021-10-31T06:00:00.000000000Z
                        32.881769076795045\t2021-10-31T06:30:00.000000000Z
                        32.881769076795045\t2021-10-31T07:00:00.000000000Z
                        97.71103146051203\t2021-10-31T07:30:00.000000000Z
                        97.71103146051203\t2021-10-31T08:00:00.000000000Z
                        81.46807944500559\t2021-10-31T08:30:00.000000000Z
                        81.46807944500559\t2021-10-31T09:00:00.000000000Z
                        57.93466326862211\t2021-10-31T09:30:00.000000000Z
                        57.93466326862211\t2021-10-31T10:00:00.000000000Z
                        12.026122412833129\t2021-10-31T10:30:00.000000000Z
                        48.820511018586934\t2021-10-31T11:00:00.000000000Z
                        48.820511018586934\t2021-10-31T11:30:00.000000000Z
                        26.922103479744898\t2021-10-31T12:00:00.000000000Z
                        26.922103479744898\t2021-10-31T12:30:00.000000000Z
                        52.98405941762054\t2021-10-31T13:00:00.000000000Z
                        52.98405941762054\t2021-10-31T13:30:00.000000000Z
                        84.45258177211063\t2021-10-31T14:00:00.000000000Z
                        84.45258177211063\t2021-10-31T14:30:00.000000000Z
                        97.5019885372507\t2021-10-31T15:00:00.000000000Z
                        97.5019885372507\t2021-10-31T15:30:00.000000000Z
                        49.00510449885239\t2021-10-31T16:00:00.000000000Z
                        49.00510449885239\t2021-10-31T16:30:00.000000000Z
                        80.01121139739173\t2021-10-31T17:00:00.000000000Z
                        80.01121139739173\t2021-10-31T17:30:00.000000000Z
                        92.050039469858\t2021-10-31T18:00:00.000000000Z
                        92.050039469858\t2021-10-31T18:30:00.000000000Z
                        45.6344569609078\t2021-10-31T19:00:00.000000000Z
                        40.455469747939254\t2021-10-31T19:30:00.000000000Z
                        40.455469747939254\t2021-10-31T20:00:00.000000000Z
                        56.594291398612405\t2021-10-31T20:30:00.000000000Z
                        56.594291398612405\t2021-10-31T21:00:00.000000000Z
                        9.750574414434398\t2021-10-31T21:30:00.000000000Z
                        9.750574414434398\t2021-10-31T22:00:00.000000000Z
                        12.105630273556178\t2021-10-31T22:30:00.000000000Z
                        12.105630273556178\t2021-10-31T23:00:00.000000000Z
                        57.78947915182423\t2021-10-31T23:30:00.000000000Z
                        57.78947915182423\t2021-11-01T00:00:00.000000000Z
                        86.85154305419587\t2021-11-01T00:30:00.000000000Z
                        86.85154305419587\t2021-11-01T01:00:00.000000000Z
                        12.02416087573498\t2021-11-01T01:30:00.000000000Z
                        12.02416087573498\t2021-11-01T02:00:00.000000000Z
                        49.42890511958454\t2021-11-01T02:30:00.000000000Z
                        49.42890511958454\t2021-11-01T03:00:00.000000000Z
                        58.912164838797885\t2021-11-01T03:30:00.000000000Z
                        67.52509547112409\t2021-11-01T04:00:00.000000000Z
                        67.52509547112409\t2021-11-01T04:30:00.000000000Z
                        44.80468966861358\t2021-11-01T05:00:00.000000000Z
                        44.80468966861358\t2021-11-01T05:30:00.000000000Z
                        89.40917126581896\t2021-11-01T06:00:00.000000000Z
                        89.40917126581896\t2021-11-01T06:30:00.000000000Z
                        94.41658975532606\t2021-11-01T07:00:00.000000000Z
                        94.41658975532606\t2021-11-01T07:30:00.000000000Z
                        62.5966045857722\t2021-11-01T08:00:00.000000000Z
                        62.5966045857722\t2021-11-01T08:30:00.000000000Z
                        94.55893004802432\t2021-11-01T09:00:00.000000000Z
                        94.55893004802432\t2021-11-01T09:30:00.000000000Z
                        21.85865835029681\t2021-11-01T10:00:00.000000000Z
                        21.85865835029681\t2021-11-01T10:30:00.000000000Z
                        3.993124821273464\t2021-11-01T11:00:00.000000000Z
                        3.993124821273464\t2021-11-01T11:30:00.000000000Z
                        84.3845956391477\t2021-11-01T12:00:00.000000000Z
                        48.92743433711657\t2021-11-01T12:30:00.000000000Z
                        48.92743433711657\t2021-11-01T13:00:00.000000000Z
                        66.97969295620055\t2021-11-01T13:30:00.000000000Z
                        66.97969295620055\t2021-11-01T14:00:00.000000000Z
                        58.93398488053903\t2021-11-01T14:30:00.000000000Z
                        """,
                "select s, to_timezone(k, 'Europe/Riga') from (select sum(a) s, k from x sample by 30m fill(prev) align to calendar time zone 'Europe/Riga')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-10-31T00:00:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(40)" +
                        ") timestamp(k) partition by NONE",
                null,
                false
        );
    }

    @Test
    public void testSampleFillPrevNotKeyedAlignToCalendarTimeZone2() throws Exception {
        // this test verifies transition from Summer to Winter time and
        // clock going backwards. An hour of time should drop out of the result set
        // without the logic trying to back fill things
        assertQuery(
                """
                        sum\tk
                        11.427984775756228\t2021-03-28T00:00:00.000000000Z
                        42.17768841969397\t2021-03-28T00:30:00.000000000Z
                        42.17768841969397\t2021-03-28T01:00:00.000000000Z
                        23.90529010846525\t2021-03-28T01:30:00.000000000Z
                        23.90529010846525\t2021-03-28T02:00:00.000000000Z
                        70.94360487171201\t2021-03-28T02:30:00.000000000Z
                        70.94360487171201\t2021-03-28T03:00:00.000000000Z
                        87.99634725391621\t2021-03-28T03:30:00.000000000Z
                        87.99634725391621\t2021-03-28T04:00:00.000000000Z
                        32.881769076795045\t2021-03-28T04:30:00.000000000Z
                        32.881769076795045\t2021-03-28T05:00:00.000000000Z
                        97.71103146051203\t2021-03-28T05:30:00.000000000Z
                        97.71103146051203\t2021-03-28T06:00:00.000000000Z
                        81.46807944500559\t2021-03-28T06:30:00.000000000Z
                        81.46807944500559\t2021-03-28T07:00:00.000000000Z
                        57.93466326862211\t2021-03-28T07:30:00.000000000Z
                        57.93466326862211\t2021-03-28T08:00:00.000000000Z
                        12.026122412833129\t2021-03-28T08:30:00.000000000Z
                        48.820511018586934\t2021-03-28T09:00:00.000000000Z
                        48.820511018586934\t2021-03-28T09:30:00.000000000Z
                        26.922103479744898\t2021-03-28T10:00:00.000000000Z
                        26.922103479744898\t2021-03-28T10:30:00.000000000Z
                        52.98405941762054\t2021-03-28T11:00:00.000000000Z
                        52.98405941762054\t2021-03-28T11:30:00.000000000Z
                        84.45258177211063\t2021-03-28T12:00:00.000000000Z
                        84.45258177211063\t2021-03-28T12:30:00.000000000Z
                        97.5019885372507\t2021-03-28T13:00:00.000000000Z
                        97.5019885372507\t2021-03-28T13:30:00.000000000Z
                        49.00510449885239\t2021-03-28T14:00:00.000000000Z
                        49.00510449885239\t2021-03-28T14:30:00.000000000Z
                        80.01121139739173\t2021-03-28T15:00:00.000000000Z
                        80.01121139739173\t2021-03-28T15:30:00.000000000Z
                        92.050039469858\t2021-03-28T16:00:00.000000000Z
                        92.050039469858\t2021-03-28T16:30:00.000000000Z
                        45.6344569609078\t2021-03-28T17:00:00.000000000Z
                        40.455469747939254\t2021-03-28T17:30:00.000000000Z
                        40.455469747939254\t2021-03-28T18:00:00.000000000Z
                        56.594291398612405\t2021-03-28T18:30:00.000000000Z
                        56.594291398612405\t2021-03-28T19:00:00.000000000Z
                        9.750574414434398\t2021-03-28T19:30:00.000000000Z
                        9.750574414434398\t2021-03-28T20:00:00.000000000Z
                        12.105630273556178\t2021-03-28T20:30:00.000000000Z
                        12.105630273556178\t2021-03-28T21:00:00.000000000Z
                        57.78947915182423\t2021-03-28T21:30:00.000000000Z
                        57.78947915182423\t2021-03-28T22:00:00.000000000Z
                        86.85154305419587\t2021-03-28T22:30:00.000000000Z
                        86.85154305419587\t2021-03-28T23:00:00.000000000Z
                        12.02416087573498\t2021-03-28T23:30:00.000000000Z
                        12.02416087573498\t2021-03-29T00:00:00.000000000Z
                        49.42890511958454\t2021-03-29T00:30:00.000000000Z
                        49.42890511958454\t2021-03-29T01:00:00.000000000Z
                        58.912164838797885\t2021-03-29T01:30:00.000000000Z
                        67.52509547112409\t2021-03-29T02:00:00.000000000Z
                        67.52509547112409\t2021-03-29T02:30:00.000000000Z
                        44.80468966861358\t2021-03-29T03:00:00.000000000Z
                        44.80468966861358\t2021-03-29T03:30:00.000000000Z
                        89.40917126581896\t2021-03-29T04:00:00.000000000Z
                        89.40917126581896\t2021-03-29T04:30:00.000000000Z
                        94.41658975532606\t2021-03-29T05:00:00.000000000Z
                        94.41658975532606\t2021-03-29T05:30:00.000000000Z
                        62.5966045857722\t2021-03-29T06:00:00.000000000Z
                        62.5966045857722\t2021-03-29T06:30:00.000000000Z
                        94.55893004802432\t2021-03-29T07:00:00.000000000Z
                        94.55893004802432\t2021-03-29T07:30:00.000000000Z
                        21.85865835029681\t2021-03-29T08:00:00.000000000Z
                        21.85865835029681\t2021-03-29T08:30:00.000000000Z
                        3.993124821273464\t2021-03-29T09:00:00.000000000Z
                        3.993124821273464\t2021-03-29T09:30:00.000000000Z
                        84.3845956391477\t2021-03-29T10:00:00.000000000Z
                        48.92743433711657\t2021-03-29T10:30:00.000000000Z
                        48.92743433711657\t2021-03-29T11:00:00.000000000Z
                        66.97969295620055\t2021-03-29T11:30:00.000000000Z
                        66.97969295620055\t2021-03-29T12:00:00.000000000Z
                        58.93398488053903\t2021-03-29T12:30:00.000000000Z
                        """,
                "select sum(a), k from x sample by 30m fill(prev) align to calendar time zone 'Europe/Riga'",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-03-28T00:00:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(40)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillPrevNotKeyedAlignToCalendarTimeZoneOffset() throws Exception {
        // this test verifies transition from Summer to Winter time and
        // clock going backwards. An hour of time should drop out of the result set
        // without the logic trying to back fill things
        assertQuery(
                """
                        sum\tk
                        11.427984775756228\t2021-10-30T23:40:00.000000000Z
                        11.427984775756228\t2021-10-31T00:10:00.000000000Z
                        66.08297852815922\t2021-10-31T00:40:00.000000000Z
                        70.94360487171201\t2021-10-31T02:40:00.000000000Z
                        70.94360487171201\t2021-10-31T03:10:00.000000000Z
                        87.99634725391621\t2021-10-31T03:40:00.000000000Z
                        87.99634725391621\t2021-10-31T04:10:00.000000000Z
                        32.881769076795045\t2021-10-31T04:40:00.000000000Z
                        32.881769076795045\t2021-10-31T05:10:00.000000000Z
                        97.71103146051203\t2021-10-31T05:40:00.000000000Z
                        81.46807944500559\t2021-10-31T06:10:00.000000000Z
                        81.46807944500559\t2021-10-31T06:40:00.000000000Z
                        57.93466326862211\t2021-10-31T07:10:00.000000000Z
                        57.93466326862211\t2021-10-31T07:40:00.000000000Z
                        12.026122412833129\t2021-10-31T08:10:00.000000000Z
                        12.026122412833129\t2021-10-31T08:40:00.000000000Z
                        48.820511018586934\t2021-10-31T09:10:00.000000000Z
                        48.820511018586934\t2021-10-31T09:40:00.000000000Z
                        26.922103479744898\t2021-10-31T10:10:00.000000000Z
                        26.922103479744898\t2021-10-31T10:40:00.000000000Z
                        52.98405941762054\t2021-10-31T11:10:00.000000000Z
                        52.98405941762054\t2021-10-31T11:40:00.000000000Z
                        84.45258177211063\t2021-10-31T12:10:00.000000000Z
                        84.45258177211063\t2021-10-31T12:40:00.000000000Z
                        97.5019885372507\t2021-10-31T13:10:00.000000000Z
                        97.5019885372507\t2021-10-31T13:40:00.000000000Z
                        49.00510449885239\t2021-10-31T14:10:00.000000000Z
                        80.01121139739173\t2021-10-31T14:40:00.000000000Z
                        80.01121139739173\t2021-10-31T15:10:00.000000000Z
                        92.050039469858\t2021-10-31T15:40:00.000000000Z
                        92.050039469858\t2021-10-31T16:10:00.000000000Z
                        45.6344569609078\t2021-10-31T16:40:00.000000000Z
                        45.6344569609078\t2021-10-31T17:10:00.000000000Z
                        40.455469747939254\t2021-10-31T17:40:00.000000000Z
                        40.455469747939254\t2021-10-31T18:10:00.000000000Z
                        56.594291398612405\t2021-10-31T18:40:00.000000000Z
                        56.594291398612405\t2021-10-31T19:10:00.000000000Z
                        9.750574414434398\t2021-10-31T19:40:00.000000000Z
                        9.750574414434398\t2021-10-31T20:10:00.000000000Z
                        12.105630273556178\t2021-10-31T20:40:00.000000000Z
                        12.105630273556178\t2021-10-31T21:10:00.000000000Z
                        57.78947915182423\t2021-10-31T21:40:00.000000000Z
                        57.78947915182423\t2021-10-31T22:10:00.000000000Z
                        86.85154305419587\t2021-10-31T22:40:00.000000000Z
                        12.02416087573498\t2021-10-31T23:10:00.000000000Z
                        12.02416087573498\t2021-10-31T23:40:00.000000000Z
                        49.42890511958454\t2021-11-01T00:10:00.000000000Z
                        49.42890511958454\t2021-11-01T00:40:00.000000000Z
                        58.912164838797885\t2021-11-01T01:10:00.000000000Z
                        58.912164838797885\t2021-11-01T01:40:00.000000000Z
                        67.52509547112409\t2021-11-01T02:10:00.000000000Z
                        67.52509547112409\t2021-11-01T02:40:00.000000000Z
                        44.80468966861358\t2021-11-01T03:10:00.000000000Z
                        44.80468966861358\t2021-11-01T03:40:00.000000000Z
                        89.40917126581896\t2021-11-01T04:10:00.000000000Z
                        89.40917126581896\t2021-11-01T04:40:00.000000000Z
                        94.41658975532606\t2021-11-01T05:10:00.000000000Z
                        94.41658975532606\t2021-11-01T05:40:00.000000000Z
                        62.5966045857722\t2021-11-01T06:10:00.000000000Z
                        62.5966045857722\t2021-11-01T06:40:00.000000000Z
                        94.55893004802432\t2021-11-01T07:10:00.000000000Z
                        21.85865835029681\t2021-11-01T07:40:00.000000000Z
                        21.85865835029681\t2021-11-01T08:10:00.000000000Z
                        3.993124821273464\t2021-11-01T08:40:00.000000000Z
                        3.993124821273464\t2021-11-01T09:10:00.000000000Z
                        84.3845956391477\t2021-11-01T09:40:00.000000000Z
                        84.3845956391477\t2021-11-01T10:10:00.000000000Z
                        48.92743433711657\t2021-11-01T10:40:00.000000000Z
                        48.92743433711657\t2021-11-01T11:10:00.000000000Z
                        66.97969295620055\t2021-11-01T11:40:00.000000000Z
                        66.97969295620055\t2021-11-01T12:10:00.000000000Z
                        58.93398488053903\t2021-11-01T12:40:00.000000000Z
                        """,
                "select sum(a), k from x sample by 30m fill(prev) align to calendar time zone 'Europe/Riga' with offset '00:40'",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-10-31T00:00:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(40)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillPrevNotKeyedEmpty() throws Exception {
        assertQuery(
                "sum\tk\n",
                "select sum(o), k from x sample by 3h fill(prev)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_int() a," +
                        " rnd_boolean() b," +
                        " rnd_str(1,1,2) c," +
                        " rnd_double(2) d," +
                        " rnd_float(2) e," +
                        " rnd_short(10,1024) f," +
                        " rnd_date(to_date('2015', 'yyyy'), to_date('2016', 'yyyy'), 2) g," +
                        " rnd_symbol(4,4,4,2) i," +
                        " rnd_long() j," +
                        " rnd_byte(2,50) l," +
                        " rnd_bin(10, 20, 2) m," +
                        " rnd_str(5,16,2) n," +
                        " rnd_double(2) o," +
                        " timestamp_sequence_ns(0, 3600000000000) p," +
                        " timestamp_sequence_ns(172800000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(0)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from " +
                        "(" +
                        "select" +
                        " rnd_int() a," +
                        " rnd_boolean() b," +
                        " rnd_str(1,1,2) c," +
                        " rnd_double(2) d," +
                        " rnd_float(2) e," +
                        " rnd_short(10,1024) f," +
                        " rnd_date(to_date('2015', 'yyyy'), to_date('2016', 'yyyy'), 2) g," +
                        " rnd_symbol(4,4,4,2) i," +
                        " rnd_long() j," +
                        " rnd_byte(2,50) l," +
                        " rnd_bin(10, 20, 2) m," +
                        " rnd_str(5,16,2) n," +
                        " rnd_double(2) o," +
                        " timestamp_sequence(0, 3600000000) p," +
                        " timestamp_sequence(172800000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        sum\tk
                        1.7032973194368575\t1970-01-03T00:00:00.000000000Z
                        1.0412323041734997\t1970-01-03T03:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillValue() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T00:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T00:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T03:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T03:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T06:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T09:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T12:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T12:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T15:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T15:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T18:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T18:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T18:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T18:00:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 3h fill(20.56)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum\tk
                        \t11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        VTJW\t42.17768841969397\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t23.90529010846525\t1970-01-03T00:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T00:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T00:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T00:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T00:00:00.000000000Z
                        \t120.87811633071126\t1970-01-03T03:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T03:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T03:00:00.000000000Z
                        PEHN\t70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T03:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T03:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T03:00:00.000000000Z
                        \t57.93466326862211\t1970-01-03T06:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T06:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T06:00:00.000000000Z
                        PEHN\t81.46807944500559\t1970-01-03T06:00:00.000000000Z
                        HYRX\t97.71103146051203\t1970-01-03T06:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T06:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T06:00:00.000000000Z
                        \t26.922103479744898\t1970-01-03T09:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T09:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T09:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T09:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T09:00:00.000000000Z
                        \t150.48604795487125\t1970-01-03T12:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T12:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T12:00:00.000000000Z
                        PEHN\t84.45258177211063\t1970-01-03T12:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T12:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T12:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T12:00:00.000000000Z
                        \t172.06125086724973\t1970-01-03T15:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T15:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T15:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T15:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T15:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T15:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T15:00:00.000000000Z
                        \t86.08992670884706\t1970-01-03T18:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T18:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T18:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T18:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T18:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T18:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T18:00:00.000000000Z
                        \t20.56\t1970-01-03T21:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T21:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T21:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T21:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T21:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T21:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T21:00:00.000000000Z
                        \t20.56\t1970-01-04T00:00:00.000000000Z
                        VTJW\t20.56\t1970-01-04T00:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-04T00:00:00.000000000Z
                        PEHN\t20.56\t1970-01-04T00:00:00.000000000Z
                        HYRX\t20.56\t1970-01-04T00:00:00.000000000Z
                        UVSD\t20.56\t1970-01-04T00:00:00.000000000Z
                        KGHV\t20.56\t1970-01-04T00:00:00.000000000Z
                        \t54.49155021518948\t1970-01-04T03:00:00.000000000Z
                        VTJW\t20.56\t1970-01-04T03:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-04T03:00:00.000000000Z
                        PEHN\t20.56\t1970-01-04T03:00:00.000000000Z
                        HYRX\t20.56\t1970-01-04T03:00:00.000000000Z
                        UVSD\t20.56\t1970-01-04T03:00:00.000000000Z
                        KGHV\t20.56\t1970-01-04T03:00:00.000000000Z
                        \t135.835983782176\t1970-01-04T06:00:00.000000000Z
                        VTJW\t20.56\t1970-01-04T06:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-04T06:00:00.000000000Z
                        PEHN\t20.56\t1970-01-04T06:00:00.000000000Z
                        HYRX\t20.56\t1970-01-04T06:00:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T06:00:00.000000000Z
                        KGHV\t20.56\t1970-01-04T06:00:00.000000000Z
                        \t20.56\t1970-01-04T09:00:00.000000000Z
                        VTJW\t20.56\t1970-01-04T09:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-04T09:00:00.000000000Z
                        PEHN\t20.56\t1970-01-04T09:00:00.000000000Z
                        HYRX\t20.56\t1970-01-04T09:00:00.000000000Z
                        UVSD\t20.56\t1970-01-04T09:00:00.000000000Z
                        KGHV\t67.52509547112409\t1970-01-04T09:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillValueAlignToCalendarTimeZone() throws Exception {
        // EST timezone has clock go back on 7 Nov. From -4 UTC to -5 UTC
        // at 6am UTC EST time is 2am (DTS), when clock goes back 7am also becomes 2am UTC
        // hence 6am UTC is duplicate timestamp and is expected to be compounded
        assertQuery(
                """
                        b\tsum\tk
                        \t11.427984775756228\t2021-11-06T22:00:00.000000000Z
                        VTJW\t101.2\t2021-11-06T22:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-06T22:00:00.000000000Z
                        PEHN\t101.2\t2021-11-06T22:00:00.000000000Z
                        HYRX\t101.2\t2021-11-06T22:00:00.000000000Z
                        \t101.2\t2021-11-06T23:00:00.000000000Z
                        VTJW\t42.17768841969397\t2021-11-06T23:00:00.000000000Z
                        RXGZ\t23.90529010846525\t2021-11-06T23:00:00.000000000Z
                        PEHN\t101.2\t2021-11-06T23:00:00.000000000Z
                        HYRX\t101.2\t2021-11-06T23:00:00.000000000Z
                        \t101.2\t2021-11-07T00:00:00.000000000Z
                        VTJW\t101.2\t2021-11-07T00:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T00:00:00.000000000Z
                        PEHN\t70.94360487171201\t2021-11-07T00:00:00.000000000Z
                        HYRX\t101.2\t2021-11-07T00:00:00.000000000Z
                        \t87.99634725391621\t2021-11-07T01:00:00.000000000Z
                        VTJW\t101.2\t2021-11-07T01:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T01:00:00.000000000Z
                        PEHN\t101.2\t2021-11-07T01:00:00.000000000Z
                        HYRX\t101.2\t2021-11-07T01:00:00.000000000Z
                        \t32.881769076795045\t2021-11-07T02:00:00.000000000Z
                        VTJW\t101.2\t2021-11-07T02:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T02:00:00.000000000Z
                        PEHN\t101.2\t2021-11-07T02:00:00.000000000Z
                        HYRX\t101.2\t2021-11-07T02:00:00.000000000Z
                        \t101.2\t2021-11-07T03:00:00.000000000Z
                        VTJW\t101.2\t2021-11-07T03:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T03:00:00.000000000Z
                        PEHN\t101.2\t2021-11-07T03:00:00.000000000Z
                        HYRX\t97.71103146051203\t2021-11-07T03:00:00.000000000Z
                        \t101.2\t2021-11-07T04:00:00.000000000Z
                        VTJW\t101.2\t2021-11-07T04:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T04:00:00.000000000Z
                        PEHN\t81.46807944500559\t2021-11-07T04:00:00.000000000Z
                        HYRX\t101.2\t2021-11-07T04:00:00.000000000Z
                        \t101.2\t2021-11-07T05:00:00.000000000Z
                        VTJW\t101.2\t2021-11-07T05:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T05:00:00.000000000Z
                        PEHN\t81.46807944500559\t2021-11-07T05:00:00.000000000Z
                        HYRX\t101.2\t2021-11-07T05:00:00.000000000Z
                        \t26.922103479744898\t2021-11-07T07:00:00.000000000Z
                        VTJW\t48.820511018586934\t2021-11-07T07:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T07:00:00.000000000Z
                        PEHN\t101.2\t2021-11-07T07:00:00.000000000Z
                        HYRX\t101.2\t2021-11-07T07:00:00.000000000Z
                        \t52.98405941762054\t2021-11-07T08:00:00.000000000Z
                        VTJW\t101.2\t2021-11-07T08:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T08:00:00.000000000Z
                        PEHN\t101.2\t2021-11-07T08:00:00.000000000Z
                        HYRX\t101.2\t2021-11-07T08:00:00.000000000Z
                        \t101.2\t2021-11-07T09:00:00.000000000Z
                        VTJW\t101.2\t2021-11-07T09:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T09:00:00.000000000Z
                        PEHN\t84.45258177211063\t2021-11-07T09:00:00.000000000Z
                        HYRX\t101.2\t2021-11-07T09:00:00.000000000Z
                        \t97.5019885372507\t2021-11-07T10:00:00.000000000Z
                        VTJW\t101.2\t2021-11-07T10:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T10:00:00.000000000Z
                        PEHN\t101.2\t2021-11-07T10:00:00.000000000Z
                        HYRX\t101.2\t2021-11-07T10:00:00.000000000Z
                        \t80.01121139739173\t2021-11-07T11:00:00.000000000Z
                        VTJW\t101.2\t2021-11-07T11:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T11:00:00.000000000Z
                        PEHN\t49.00510449885239\t2021-11-07T11:00:00.000000000Z
                        HYRX\t101.2\t2021-11-07T11:00:00.000000000Z
                        \t92.050039469858\t2021-11-07T12:00:00.000000000Z
                        VTJW\t101.2\t2021-11-07T12:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T12:00:00.000000000Z
                        PEHN\t101.2\t2021-11-07T12:00:00.000000000Z
                        HYRX\t101.2\t2021-11-07T12:00:00.000000000Z
                        \t45.6344569609078\t2021-11-07T13:00:00.000000000Z
                        VTJW\t101.2\t2021-11-07T13:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T13:00:00.000000000Z
                        PEHN\t101.2\t2021-11-07T13:00:00.000000000Z
                        HYRX\t101.2\t2021-11-07T13:00:00.000000000Z
                        \t40.455469747939254\t2021-11-07T14:00:00.000000000Z
                        VTJW\t101.2\t2021-11-07T14:00:00.000000000Z
                        RXGZ\t101.2\t2021-11-07T14:00:00.000000000Z
                        PEHN\t101.2\t2021-11-07T14:00:00.000000000Z
                        HYRX\t101.2\t2021-11-07T14:00:00.000000000Z
                        """,
                "select b, sum(a), k from x sample by 1h fill(101.2) align to calendar time zone 'EST'",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-11-06T22:10:00.000000000Z' as timestamp_ns), 3100000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillValueAllKeyTypes() throws Exception {
        assertQuery(
                """
                        b\th\ti\tj\tl\tsum\tsum1\tsum2\tsum3\tsum4\tsum5\tk
                        \tFFYUDEYY\t00000000 49 b4 59 7e 3b 08 a1 1e 38 8d 1b 9e f4 c8 39 09\t2015-09-16T21:59:49.857Z\tfalse\t11.427984775756228\t42.177685\t1432278050\t13216\t4\t5539350449504785212\t1970-01-03T00:00:00.000000000Z
                        HYRX\tGETJR\t\t2015-04-09T11:42:28.332Z\tfalse\t12.026122412833129\t48.820507\t458818940\t3282\t8\t-6253307669002054137\t1970-01-03T00:00:00.000000000Z
                        \tZVDZJ\t00000000 e3 f1 f1 1e ca 9c 1d 06 ac 37 c8 cd 82 89 2b 4d\t2015-08-26T10:57:26.275Z\ttrue\t5.048190020054388\t0.11075139\t66297136\t-5637\t7\t9036423629723776443\t1970-01-03T00:00:00.000000000Z
                        \tLYXWCK\t00000000 47 dc d2 85 7f a5 b8 7b 4a 9d 46 7c 8d\t2015-07-13T12:15:31.895Z\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        \t\t00000000 49 1c f2 3c ed 39 ac a8 3b a6\t2015-01-08T06:16:03.023Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        RXGZ\tVLJUM\t00000000 29 5e 69 c6 eb ea c3 c9 73 93 46 fe\t2015-06-28T03:15:43.251Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        \tHWVDKF\t00000000 f5 5d d0 eb 67 44 a7 6a 71 34 e0\t2015-12-05T03:07:39.553Z\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        HYRX\tNZHZS\t\t2015-10-11T07:06:57.173Z\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        RXGZ\tEBNDCQCE\t00000000 e9 0c ea 4e ea 8b f5 0f 2d b3\t2015-03-25T11:25:58.599Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        \tUIZUL\t00000000 54 52 d0 29 26 c5 aa da 18 ce 5f b2 8b 5c 54 90\t\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        \tFFYUDEYY\t00000000 49 b4 59 7e 3b 08 a1 1e 38 8d 1b 9e f4 c8 39 09\t2015-09-16T21:59:49.857Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        HYRX\tGETJR\t\t2015-04-09T11:42:28.332Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        \tZVDZJ\t00000000 e3 f1 f1 1e ca 9c 1d 06 ac 37 c8 cd 82 89 2b 4d\t2015-08-26T10:57:26.275Z\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        \tLYXWCK\t00000000 47 dc d2 85 7f a5 b8 7b 4a 9d 46 7c 8d\t2015-07-13T12:15:31.895Z\ttrue\t11.585982949541474\t81.64182\t998315423\t-5585\t7\t8587391969565958670\t1970-01-03T03:00:00.000000000Z
                        \t\t00000000 49 1c f2 3c ed 39 ac a8 3b a6\t2015-01-08T06:16:03.023Z\tfalse\t19.751370382305055\t68.06873\t544695670\t-1464\t6\t-5024542231726589509\t1970-01-03T03:00:00.000000000Z
                        RXGZ\tVLJUM\t00000000 29 5e 69 c6 eb ea c3 c9 73 93 46 fe\t2015-06-28T03:15:43.251Z\tfalse\t84.3845956391477\t48.927433\t1100812407\t-32358\t10\t5398991075259361292\t1970-01-03T03:00:00.000000000Z
                        \tHWVDKF\t00000000 f5 5d d0 eb 67 44 a7 6a 71 34 e0\t2015-12-05T03:07:39.553Z\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        HYRX\tNZHZS\t\t2015-10-11T07:06:57.173Z\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        RXGZ\tEBNDCQCE\t00000000 e9 0c ea 4e ea 8b f5 0f 2d b3\t2015-03-25T11:25:58.599Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        \tUIZUL\t00000000 54 52 d0 29 26 c5 aa da 18 ce 5f b2 8b 5c 54 90\t\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        \tFFYUDEYY\t00000000 49 b4 59 7e 3b 08 a1 1e 38 8d 1b 9e f4 c8 39 09\t2015-09-16T21:59:49.857Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        HYRX\tGETJR\t\t2015-04-09T11:42:28.332Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        \tZVDZJ\t00000000 e3 f1 f1 1e ca 9c 1d 06 ac 37 c8 cd 82 89 2b 4d\t2015-08-26T10:57:26.275Z\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        \tLYXWCK\t00000000 47 dc d2 85 7f a5 b8 7b 4a 9d 46 7c 8d\t2015-07-13T12:15:31.895Z\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        \t\t00000000 49 1c f2 3c ed 39 ac a8 3b a6\t2015-01-08T06:16:03.023Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        RXGZ\tVLJUM\t00000000 29 5e 69 c6 eb ea c3 c9 73 93 46 fe\t2015-06-28T03:15:43.251Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        \tHWVDKF\t00000000 f5 5d d0 eb 67 44 a7 6a 71 34 e0\t2015-12-05T03:07:39.553Z\ttrue\t85.93131480724348\t10.527277\t2105201404\t5667\t8\t-8994301462266164776\t1970-01-03T06:00:00.000000000Z
                        HYRX\tNZHZS\t\t2015-10-11T07:06:57.173Z\ttrue\t63.412928948436154\t5.024612\t1377625589\t-25710\t3\t2151565237758036093\t1970-01-03T06:00:00.000000000Z
                        RXGZ\tEBNDCQCE\t00000000 e9 0c ea 4e ea 8b f5 0f 2d b3\t2015-03-25T11:25:58.599Z\tfalse\t85.84308438045007\t54.669006\t903066492\t-2990\t4\t-1134031357796740497\t1970-01-03T06:00:00.000000000Z
                        \tUIZUL\t00000000 54 52 d0 29 26 c5 aa da 18 ce 5f b2 8b 5c 54 90\t\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        \tFFYUDEYY\t00000000 49 b4 59 7e 3b 08 a1 1e 38 8d 1b 9e f4 c8 39 09\t2015-09-16T21:59:49.857Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        HYRX\tGETJR\t\t2015-04-09T11:42:28.332Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        \tZVDZJ\t00000000 e3 f1 f1 1e ca 9c 1d 06 ac 37 c8 cd 82 89 2b 4d\t2015-08-26T10:57:26.275Z\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        \tLYXWCK\t00000000 47 dc d2 85 7f a5 b8 7b 4a 9d 46 7c 8d\t2015-07-13T12:15:31.895Z\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        \t\t00000000 49 1c f2 3c ed 39 ac a8 3b a6\t2015-01-08T06:16:03.023Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        RXGZ\tVLJUM\t00000000 29 5e 69 c6 eb ea c3 c9 73 93 46 fe\t2015-06-28T03:15:43.251Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        \tHWVDKF\t00000000 f5 5d d0 eb 67 44 a7 6a 71 34 e0\t2015-12-05T03:07:39.553Z\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        HYRX\tNZHZS\t\t2015-10-11T07:06:57.173Z\ttrue\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        RXGZ\tEBNDCQCE\t00000000 e9 0c ea 4e ea 8b f5 0f 2d b3\t2015-03-25T11:25:58.599Z\tfalse\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        \tUIZUL\t00000000 54 52 d0 29 26 c5 aa da 18 ce 5f b2 8b 5c 54 90\t\ttrue\t21.485589614090927\t6.2027454\t358259591\t-29980\t8\t-8841102831894340636\t1970-01-03T09:00:00.000000000Z
                        """,
                "select b, h, i, j, l, sum(a), sum(c), sum(d), sum(e), sum(f), sum(g), k from x sample by 3h fill(20.56, 0, 0, 0, 0, 0)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " rnd_float(0)*100 c," +
                        " abs(rnd_int()) d," +
                        " rnd_short() e," +
                        " rnd_byte(3,10) f," +
                        " rnd_long() g," +
                        " rnd_str(5,8,2) h," +
                        " rnd_bin(10, 20, 2) i," +
                        " rnd_date(to_date('2015', 'yyyy'), to_date('2016', 'yyyy'), 2) j," +
                        " rnd_boolean() l," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(10)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillValueAllTypes() throws Exception {
        assertQuery(
                """
                        b\tlast\tlast1\tlast2\tlast3\tlast4\tlast5\tk
                        \t62.76954028373309\t70.9436\t1125169127\t-12348\t8\t6600081143067978388\t1970-01-03T00:00:00.000000000Z
                        CPSW\t0.35983672154330515\t76.75673\t113506296\t27809\t9\t-8889930662239044040\t1970-01-03T00:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        \t76.64256753596138\t55.224937\t326010667\t-5741\t8\t7392877322819819290\t1970-01-03T03:00:00.000000000Z
                        CPSW\t13.450170570900255\t34.35685\t410717394\t18229\t10\t6820495939660535106\t1970-01-03T03:00:00.000000000Z
                        PEHN\t15.786635599554755\t12.50304\t264240638\t-7976\t6\t-8480005421611953360\t1970-01-03T03:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        \t80.01121139739173\t92.05003\t235358133\t-9039\t5\t6473208488991371747\t1970-01-03T06:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        PEHN\t86.64158914718531\t88.374214\t1566901076\t-3017\t3\t-5028301966399563827\t1970-01-03T06:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        \t97.03060808244088\t91.01417\t1794809330\t10028\t4\t-5512653573876168745\t1970-01-03T09:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t3.831785863680992\t42.02044\t1254404167\t1756\t5\t8702525427024484485\t1970-01-03T09:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        \t89.40917126581896\t94.41659\t2124174232\t2508\t9\t-7103100524321179064\t1970-01-03T12:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T12:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T12:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T12:00:00.000000000Z
                        HYRX\t24.008362859107102\t76.57837\t2111250190\t-13252\t8\t7973684666911773753\t1970-01-03T12:00:00.000000000Z
                        \t26.369335635512837\t56.994446\t2011884585\t9054\t10\t-5315599072928175674\t1970-01-03T15:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T15:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T15:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T15:00:00.000000000Z
                        HYRX\t2.6836863013701473\t10.643042\t502711083\t-8221\t9\t-7709579215942154242\t1970-01-03T15:00:00.000000000Z
                        \t42.74704286353759\t33.74707\t684778036\t11524\t6\t7574443524652611981\t1970-01-03T18:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        """,
                "select b, last(a), last(c), last(d), last(e), last(f), last(g), k from x sample by 3h fill(20.56, 0, 0, 0, 0, 0)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " rnd_float(0)*100 c," +
                        " abs(rnd_int()) d," +
                        " rnd_short() e," +
                        " rnd_byte(3,10) f," +
                        " rnd_long() g," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " rnd_float(0)*100 c," +
                        " abs(rnd_int()) d," +
                        " rnd_short() e," +
                        " rnd_byte(3,10) f," +
                        " rnd_long() g," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tlast\tlast1\tlast2\tlast3\tlast4\tlast5\tk
                        \t62.76954028373309\t70.9436\t1125169127\t-12348\t8\t6600081143067978388\t1970-01-03T00:00:00.000000000Z
                        CPSW\t0.35983672154330515\t76.75673\t113506296\t27809\t9\t-8889930662239044040\t1970-01-03T00:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        EZGH\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        FLOP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        WVDK\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        JOXP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        \t76.64256753596138\t55.224937\t326010667\t-5741\t8\t7392877322819819290\t1970-01-03T03:00:00.000000000Z
                        CPSW\t13.450170570900255\t34.35685\t410717394\t18229\t10\t6820495939660535106\t1970-01-03T03:00:00.000000000Z
                        PEHN\t15.786635599554755\t12.50304\t264240638\t-7976\t6\t-8480005421611953360\t1970-01-03T03:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        EZGH\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        FLOP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        WVDK\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        JOXP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        \t80.01121139739173\t92.05003\t235358133\t-9039\t5\t6473208488991371747\t1970-01-03T06:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        PEHN\t86.64158914718531\t88.374214\t1566901076\t-3017\t3\t-5028301966399563827\t1970-01-03T06:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        EZGH\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        FLOP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        WVDK\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        JOXP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        \t97.03060808244088\t91.01417\t1794809330\t10028\t4\t-5512653573876168745\t1970-01-03T09:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t3.831785863680992\t42.02044\t1254404167\t1756\t5\t8702525427024484485\t1970-01-03T09:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        EZGH\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        FLOP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        WVDK\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        JOXP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        \t89.40917126581896\t94.41659\t2124174232\t2508\t9\t-7103100524321179064\t1970-01-03T12:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T12:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T12:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T12:00:00.000000000Z
                        HYRX\t24.008362859107102\t76.57837\t2111250190\t-13252\t8\t7973684666911773753\t1970-01-03T12:00:00.000000000Z
                        EZGH\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T12:00:00.000000000Z
                        FLOP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T12:00:00.000000000Z
                        WVDK\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T12:00:00.000000000Z
                        JOXP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T12:00:00.000000000Z
                        \t26.369335635512837\t56.994446\t2011884585\t9054\t10\t-5315599072928175674\t1970-01-03T15:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T15:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T15:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T15:00:00.000000000Z
                        HYRX\t2.6836863013701473\t10.643042\t502711083\t-8221\t9\t-7709579215942154242\t1970-01-03T15:00:00.000000000Z
                        EZGH\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T15:00:00.000000000Z
                        FLOP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T15:00:00.000000000Z
                        WVDK\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T15:00:00.000000000Z
                        JOXP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T15:00:00.000000000Z
                        \t42.74704286353759\t33.74707\t684778036\t11524\t6\t7574443524652611981\t1970-01-03T18:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        EZGH\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        FLOP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        WVDK\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        JOXP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        \t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T21:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T21:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T21:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T21:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T21:00:00.000000000Z
                        EZGH\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T21:00:00.000000000Z
                        FLOP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T21:00:00.000000000Z
                        WVDK\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T21:00:00.000000000Z
                        JOXP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T21:00:00.000000000Z
                        \t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T00:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T00:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T00:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T00:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T00:00:00.000000000Z
                        EZGH\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T00:00:00.000000000Z
                        FLOP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T00:00:00.000000000Z
                        WVDK\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T00:00:00.000000000Z
                        JOXP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T00:00:00.000000000Z
                        \t20.585069039325443\t98.8401\t1278547815\t17250\t3\t-6703401424236463520\t1970-01-04T03:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T03:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T03:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T03:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T03:00:00.000000000Z
                        EZGH\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T03:00:00.000000000Z
                        FLOP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T03:00:00.000000000Z
                        WVDK\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T03:00:00.000000000Z
                        JOXP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T03:00:00.000000000Z
                        \t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T06:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T06:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T06:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T06:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T06:00:00.000000000Z
                        EZGH\t5.0246156790690115\t38.422543\t370796356\t5422\t3\t4959459375462458218\t1970-01-04T06:00:00.000000000Z
                        FLOP\t17.180291960857296\t5.158454\t532016913\t-3028\t7\t2282781332678491916\t1970-01-04T06:00:00.000000000Z
                        WVDK\t54.66900921405317\t35.681107\t874367915\t-23001\t10\t9089874911309539983\t1970-01-04T06:00:00.000000000Z
                        JOXP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T06:00:00.000000000Z
                        \t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T09:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T09:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T09:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T09:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T09:00:00.000000000Z
                        EZGH\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T09:00:00.000000000Z
                        FLOP\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T09:00:00.000000000Z
                        WVDK\t20.56\t0.0\t0\t0\t0\t0\t1970-01-04T09:00:00.000000000Z
                        JOXP\t67.29405590773638\t76.06252\t1165635863\t2316\t9\t-4547802916868961458\t1970-01-04T09:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillValueAllTypesAndTruncate() throws Exception {
        assertMemoryLeak(() -> {
            execute(
                    "create table x as " +
                            "(" +
                            "select" +
                            " rnd_double(0)*100 a," +
                            " rnd_symbol(5,4,4,1) b," +
                            " rnd_float(0)*100 c," +
                            " abs(rnd_int()) d," +
                            " rnd_short() e," +
                            " rnd_byte(3,10) f," +
                            " rnd_long() g," +
                            " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                            " from" +
                            " long_sequence(20)" +
                            ") timestamp(k) partition by NONE"
            );

            snapshotMemoryUsage();

            try (final RecordCursorFactory factory = select("select b, sum(a), sum(c), sum(d), sum(e), sum(f), sum(g), k from x sample by 3h fill(20.56, 0, 0, 0, 0, 0)")) {
                assertTimestamp("k", factory);
                String expected = """
                        b\tsum\tsum1\tsum2\tsum3\tsum4\tsum5\tk
                        \t74.19752505948932\t113.12129\t2557447177\t868\t12\t-6307312481136788016\t1970-01-03T00:00:00.000000000Z
                        CPSW\t0.35983672154330515\t76.75673\t113506296\t27809\t9\t-8889930662239044040\t1970-01-03T00:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T00:00:00.000000000Z
                        \t76.64256753596138\t55.224937\t326010667\t-5741\t8\t7392877322819819290\t1970-01-03T03:00:00.000000000Z
                        CPSW\t13.450170570900255\t34.35685\t410717394\t18229\t10\t6820495939660535106\t1970-01-03T03:00:00.000000000Z
                        PEHN\t15.786635599554755\t12.50304\t264240638\t-7976\t6\t-8480005421611953360\t1970-01-03T03:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T03:00:00.000000000Z
                        \t85.05940141744613\t92.16078\t301655269\t-14676\t12\t-2937111954994403426\t1970-01-03T06:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        PEHN\t86.64158914718531\t88.374214\t1566901076\t-3017\t3\t-5028301966399563827\t1970-01-03T06:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T06:00:00.000000000Z
                        \t106.78118249687527\t103.1198\t3029605432\t-2372\t12\t-1162868573414266742\t1970-01-03T09:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        RXGZ\t3.831785863680992\t42.02044\t1254404167\t1756\t5\t8702525427024484485\t1970-01-03T09:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T09:00:00.000000000Z
                        \t117.60937843256664\t189.81728\t3717804370\t-27064\t17\t2215137494070785317\t1970-01-03T12:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T12:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T12:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T12:00:00.000000000Z
                        HYRX\t24.008362859107102\t76.57837\t2111250190\t-13252\t8\t7973684666911773753\t1970-01-03T12:00:00.000000000Z
                        \t28.087836621126815\t139.30695\t2587989045\t11751\t17\t-8594661640328306402\t1970-01-03T15:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T15:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T15:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T15:00:00.000000000Z
                        HYRX\t2.6836863013701473\t10.643042\t502711083\t-8221\t9\t-7709579215942154242\t1970-01-03T15:00:00.000000000Z
                        \t75.17160551750754\t120.51888\t2362241402\t514\t11\t-2863260545700031392\t1970-01-03T18:00:00.000000000Z
                        CPSW\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        PEHN\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        RXGZ\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        HYRX\t20.56\t0.0\t0\t0\t0\t0\t1970-01-03T18:00:00.000000000Z
                        """;

                assertCursor(expected, factory, false, false, false);
                // make sure we get the same outcome when we get factory to create new cursor
                assertCursor(expected, factory, false, false, false);
                // make sure strings, binary fields and symbols are compliant with expected record behaviour
                assertVariableColumns(factory, sqlExecutionContext);

                execute("truncate table x");
                try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                    println(factory, cursor);
                    assertEquals("b\tsum\tsum1\tsum2\tsum3\tsum4\tsum5\tk\n", sink);
                }
            }
        });
    }

    @Test
    public void testSampleFillValueBadType() throws Exception {
        assertException(
                "select b, sum_t(b), k from x sample by 3h fill(20.56)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_str(1,1,2) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                10,
                "Unsupported type"
        );
    }

    @Test
    public void testSampleFillValueEmpty() throws Exception {
        assertQuery(
                "b\tsum\tk\n",
                "select b, sum(a), k from x sample by 3h fill(20.56)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(0)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillValueFromSubQuery() throws Exception {
        assertQuery(
                """
                        b\tsum\tk
                        RXGZ\t23.90529010846525\t1970-01-03T02:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T02:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T02:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T02:00:00.000000000Z
                        \t20.56\t1970-01-03T02:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T05:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T05:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T05:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T05:00:00.000000000Z
                        \t20.56\t1970-01-03T05:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T08:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T08:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T08:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T08:00:00.000000000Z
                        \t20.56\t1970-01-03T08:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T11:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T11:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T11:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T11:00:00.000000000Z
                        \t20.56\t1970-01-03T11:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T14:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T14:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T14:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T14:00:00.000000000Z
                        \t20.56\t1970-01-03T14:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T17:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T17:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T17:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T17:00:00.000000000Z
                        \t40.455469747939254\t1970-01-03T17:00:00.000000000Z
                        """,
                "select b, sum(a), k from (x latest on k partition by b) sample by 3h fill(20.56) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from (" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(277200000000, 3600000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k)",
                """
                        b\tsum\tk
                        RXGZ\t23.90529010846525\t1970-01-03T02:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T02:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T02:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T02:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T02:00:00.000000000Z
                        \t20.56\t1970-01-03T02:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T02:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T05:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T05:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T05:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T05:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T05:00:00.000000000Z
                        \t20.56\t1970-01-03T05:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T05:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T08:00:00.000000000Z
                        HYRX\t12.026122412833129\t1970-01-03T08:00:00.000000000Z
                        VTJW\t48.820511018586934\t1970-01-03T08:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T08:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T08:00:00.000000000Z
                        \t20.56\t1970-01-03T08:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T08:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T11:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T11:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T11:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T11:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T11:00:00.000000000Z
                        \t20.56\t1970-01-03T11:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T11:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T14:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T14:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T14:00:00.000000000Z
                        PEHN\t49.00510449885239\t1970-01-03T14:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T14:00:00.000000000Z
                        \t20.56\t1970-01-03T14:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T14:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T17:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T17:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T17:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T17:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T17:00:00.000000000Z
                        \t20.56\t1970-01-03T17:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T17:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T20:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T20:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T20:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T20:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T20:00:00.000000000Z
                        \t20.56\t1970-01-03T20:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T20:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-03T23:00:00.000000000Z
                        HYRX\t20.56\t1970-01-03T23:00:00.000000000Z
                        VTJW\t20.56\t1970-01-03T23:00:00.000000000Z
                        PEHN\t20.56\t1970-01-03T23:00:00.000000000Z
                        UVSD\t20.56\t1970-01-03T23:00:00.000000000Z
                        \t20.56\t1970-01-03T23:00:00.000000000Z
                        KGHV\t20.56\t1970-01-03T23:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-04T02:00:00.000000000Z
                        HYRX\t20.56\t1970-01-04T02:00:00.000000000Z
                        VTJW\t20.56\t1970-01-04T02:00:00.000000000Z
                        PEHN\t20.56\t1970-01-04T02:00:00.000000000Z
                        UVSD\t20.56\t1970-01-04T02:00:00.000000000Z
                        \t20.56\t1970-01-04T02:00:00.000000000Z
                        KGHV\t20.56\t1970-01-04T02:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-04T05:00:00.000000000Z
                        HYRX\t20.56\t1970-01-04T05:00:00.000000000Z
                        VTJW\t20.56\t1970-01-04T05:00:00.000000000Z
                        PEHN\t20.56\t1970-01-04T05:00:00.000000000Z
                        UVSD\t49.42890511958454\t1970-01-04T05:00:00.000000000Z
                        \t20.56\t1970-01-04T05:00:00.000000000Z
                        KGHV\t20.56\t1970-01-04T05:00:00.000000000Z
                        RXGZ\t20.56\t1970-01-04T08:00:00.000000000Z
                        HYRX\t20.56\t1970-01-04T08:00:00.000000000Z
                        VTJW\t20.56\t1970-01-04T08:00:00.000000000Z
                        PEHN\t20.56\t1970-01-04T08:00:00.000000000Z
                        UVSD\t20.56\t1970-01-04T08:00:00.000000000Z
                        \t58.912164838797885\t1970-01-04T08:00:00.000000000Z
                        KGHV\t67.52509547112409\t1970-01-04T08:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillValueInvalid() throws Exception {
        assertException(
                "select b, sum_t(a), sum(c), sum(d), sum(e), sum(f), sum(g), k from x sample by 3h fill(20.56, none, 0, 0, 0)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " rnd_float(0)*100 c," +
                        " abs(rnd_int()) d," +
                        " rnd_short() e," +
                        " rnd_byte(3,10) f," +
                        " rnd_long() g," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                94,
                "invalid fill value"
        );
    }

    @Test
    public void testSampleFillValueListWithLinearAllTypesNotKeyed() throws Exception {
        assertQuery(
                "first\tfirst1\tmin\tmax\tfirst2\tsum\tfirst3\tfirst4\tk\n",
                "select first(a),first(c),min(d),max(e),first(f),sum(j),first(l),first(p), k " +
                        "from x sample by 15m fill(linear,linear,linear,linear,linear,linear,linear,linear)",
                "create table x " +
                        "(" +
                        "a int," +
                        " c char," +
                        " d double," +
                        " e float," +
                        " f short," +
                        " j long," +
                        " l byte," +
                        " p timestamp_ns," +
                        " k timestamp_ns" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select" +
                        " rnd_int() a," +
                        " rnd_char() c," +
                        " rnd_double(2) d," +
                        " rnd_float(2) e," +
                        " rnd_short(10,1024) f," +
                        " rnd_long() j," +
                        " rnd_byte(2,50) l," +
                        " timestamp_sequence(0, 3600000000) p," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(5)",
                """
                        first\tfirst1\tmin\tmax\tfirst2\tsum\tfirst3\tfirst4\tk
                        -1148479920\tT\tnull\t0.08486962\t635\t-7611843578141082998\t45\t1970-01-01T00:00:00.000000000Z\t1970-01-03T00:00:00.000000000Z
                        -394179013\tU\tnull\t0.3100595\t615\t-7444863803039211520\t35\t1970-01-01T00:00:00.000000000Z\t1970-01-03T00:15:00.000000000Z
                        360121893\tV\tnull\t0.53524935\t596\t-7277884027937341440\t25\t1970-01-01T00:00:00.000000000Z\t1970-01-03T00:30:00.000000000Z
                        1114422799\tW\tnull\t0.7604392\t576\t-7110904252835471360\t15\t1970-01-01T00:00:00.000000000Z\t1970-01-03T00:45:00.000000000Z
                        1868723706\tY\t0.13123360041292131\t0.9856291\t557\t-6943924477733600060\t5\t1970-01-01T01:00:00.000000000Z\t1970-01-03T01:00:00.000000000Z
                        1299079178\tY\tnull\t0.92949754\t524\t-6204257507692568576\t7\t1970-01-01T01:00:00.000000000Z\t1970-01-03T01:15:00.000000000Z
                        729434650\tY\tnull\t0.873366\t492\t-5464590537651535872\t10\t1970-01-01T01:00:00.000000000Z\t1970-01-03T01:30:00.000000000Z
                        159790122\tY\tnull\t0.81723446\t460\t-4724923567610504192\t12\t1970-01-01T01:00:00.000000000Z\t1970-01-03T01:45:00.000000000Z
                        -409854405\tZ\tnull\t0.7611029\t428\t-3985256597569472057\t15\t1970-01-01T02:00:00.000000000Z\t1970-01-03T02:00:00.000000000Z
                        -665460316\tX\tnull\t0.67643607\t452\t-4857888593628719104\t18\t1970-01-01T02:00:00.000000000Z\t1970-01-03T02:15:00.000000000Z
                        -921066227\tW\tnull\t0.5917693\t476\t-5730520589687966720\t21\t1970-01-01T02:00:00.000000000Z\t1970-01-03T02:30:00.000000000Z
                        -1176672138\tU\tnull\t0.5071025\t500\t-6603152585747214336\t24\t1970-01-01T02:00:00.000000000Z\t1970-01-03T02:45:00.000000000Z
                        -1432278050\tT\t0.7261136209823622\t0.42243564\t524\t-7475784581806461658\t27\t1970-01-01T03:00:00.000000000Z\t1970-01-03T03:00:00.000000000Z
                        -1355500819\tP\t0.59422476067067\t0.40084738\t631\t-4617290017241081856\t21\t1970-01-01T03:00:00.000000000Z\t1970-01-03T03:15:00.000000000Z
                        -1278723588\tL\t0.4623359003589777\t0.37925908\t738\t-1758795452675700736\t15\t1970-01-01T03:00:00.000000000Z\t1970-01-03T03:30:00.000000000Z
                        -1201946357\tH\t0.3304470400472854\t0.35767078\t845\t1099699111889679360\t9\t1970-01-01T03:00:00.000000000Z\t1970-01-03T03:45:00.000000000Z
                        -1125169127\tE\t0.1985581797355932\t0.33608252\t953\t3958193676455060057\t3\t1970-01-01T04:00:00.000000000Z\t1970-01-03T04:00:00.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillValueListWithNullAndPrev() throws Exception {
        assertQuery(
                """
                        b\tsum\tcount\tmin\tmax\tavg\tk
                        XYZ\t28.45577791213847\t1\t28.45577791213847\t28.45577791213847\t28.45577791213847\t1970-01-03T01:00:00.000000000Z
                        ABC\t20.56\tnull\tnull\tnull\tnull\t1970-01-03T01:00:00.000000000Z
                        XYZ\t20.56\tnull\t28.45577791213847\t28.45577791213847\t28.45577791213847\t1970-01-03T01:30:00.000000000Z
                        ABC\t20.56\tnull\tnull\tnull\tnull\t1970-01-03T01:30:00.000000000Z
                        XYZ\t20.56\tnull\t28.45577791213847\t28.45577791213847\t28.45577791213847\t1970-01-03T02:00:00.000000000Z
                        ABC\t20.56\tnull\tnull\tnull\tnull\t1970-01-03T02:00:00.000000000Z
                        XYZ\t20.56\tnull\t28.45577791213847\t28.45577791213847\t28.45577791213847\t1970-01-03T02:30:00.000000000Z
                        ABC\t20.56\tnull\tnull\tnull\tnull\t1970-01-03T02:30:00.000000000Z
                        XYZ\t20.56\tnull\t28.45577791213847\t28.45577791213847\t28.45577791213847\t1970-01-03T03:00:00.000000000Z
                        ABC\t79.05675319675964\t1\t79.05675319675964\t79.05675319675964\t79.05675319675964\t1970-01-03T03:00:00.000000000Z
                        XYZ\t20.56\tnull\t28.45577791213847\t28.45577791213847\t28.45577791213847\t1970-01-03T03:30:00.000000000Z
                        ABC\t20.56\tnull\t79.05675319675964\t79.05675319675964\t79.05675319675964\t1970-01-03T03:30:00.000000000Z
                        XYZ\t20.56\tnull\t28.45577791213847\t28.45577791213847\t28.45577791213847\t1970-01-03T04:00:00.000000000Z
                        ABC\t20.56\tnull\t79.05675319675964\t79.05675319675964\t79.05675319675964\t1970-01-03T04:00:00.000000000Z
                        XYZ\t20.56\tnull\t28.45577791213847\t28.45577791213847\t28.45577791213847\t1970-01-03T04:30:00.000000000Z
                        ABC\t20.56\tnull\t79.05675319675964\t79.05675319675964\t79.05675319675964\t1970-01-03T04:30:00.000000000Z
                        XYZ\t11.427984775756228\t1\t11.427984775756228\t11.427984775756228\t11.427984775756228\t1970-01-03T05:00:00.000000000Z
                        ABC\t20.56\tnull\t79.05675319675964\t79.05675319675964\t79.05675319675964\t1970-01-03T05:00:00.000000000Z
                        XYZ\t20.56\tnull\t11.427984775756228\t11.427984775756228\t11.427984775756228\t1970-01-03T05:30:00.000000000Z
                        ABC\t20.56\tnull\t79.05675319675964\t79.05675319675964\t79.05675319675964\t1970-01-03T05:30:00.000000000Z
                        XYZ\t20.56\tnull\t11.427984775756228\t11.427984775756228\t11.427984775756228\t1970-01-03T06:00:00.000000000Z
                        ABC\t20.56\tnull\t79.05675319675964\t79.05675319675964\t79.05675319675964\t1970-01-03T06:00:00.000000000Z
                        XYZ\t20.56\tnull\t11.427984775756228\t11.427984775756228\t11.427984775756228\t1970-01-03T06:30:00.000000000Z
                        ABC\t20.56\tnull\t79.05675319675964\t79.05675319675964\t79.05675319675964\t1970-01-03T06:30:00.000000000Z
                        XYZ\t72.61136209823621\t1\t72.61136209823621\t72.61136209823621\t72.61136209823621\t1970-01-03T07:00:00.000000000Z
                        ABC\t20.56\tnull\t79.05675319675964\t79.05675319675964\t79.05675319675964\t1970-01-03T07:00:00.000000000Z
                        XYZ\t20.56\tnull\t72.61136209823621\t72.61136209823621\t72.61136209823621\t1970-01-03T07:30:00.000000000Z
                        ABC\t20.56\tnull\t79.05675319675964\t79.05675319675964\t79.05675319675964\t1970-01-03T07:30:00.000000000Z
                        XYZ\t20.56\tnull\t72.61136209823621\t72.61136209823621\t72.61136209823621\t1970-01-03T08:00:00.000000000Z
                        ABC\t20.56\tnull\t79.05675319675964\t79.05675319675964\t79.05675319675964\t1970-01-03T08:00:00.000000000Z
                        XYZ\t20.56\tnull\t72.61136209823621\t72.61136209823621\t72.61136209823621\t1970-01-03T08:30:00.000000000Z
                        ABC\t20.56\tnull\t79.05675319675964\t79.05675319675964\t79.05675319675964\t1970-01-03T08:30:00.000000000Z
                        XYZ\t20.56\tnull\t72.61136209823621\t72.61136209823621\t72.61136209823621\t1970-01-03T09:00:00.000000000Z
                        ABC\t87.56771741121929\t1\t87.56771741121929\t87.56771741121929\t87.56771741121929\t1970-01-03T09:00:00.000000000Z
                        XYZ\t20.56\tnull\t72.61136209823621\t72.61136209823621\t72.61136209823621\t1970-01-03T09:30:00.000000000Z
                        ABC\t20.56\tnull\t87.56771741121929\t87.56771741121929\t87.56771741121929\t1970-01-03T09:30:00.000000000Z
                        XYZ\t20.56\tnull\t72.61136209823621\t72.61136209823621\t72.61136209823621\t1970-01-03T10:00:00.000000000Z
                        ABC\t20.56\tnull\t87.56771741121929\t87.56771741121929\t87.56771741121929\t1970-01-03T10:00:00.000000000Z
                        XYZ\t20.56\tnull\t72.61136209823621\t72.61136209823621\t72.61136209823621\t1970-01-03T10:30:00.000000000Z
                        ABC\t20.56\tnull\t87.56771741121929\t87.56771741121929\t87.56771741121929\t1970-01-03T10:30:00.000000000Z
                        XYZ\t20.56\tnull\t72.61136209823621\t72.61136209823621\t72.61136209823621\t1970-01-03T11:00:00.000000000Z
                        ABC\t69.05404443676369\t1\t69.05404443676369\t69.05404443676369\t69.05404443676369\t1970-01-03T11:00:00.000000000Z
                        XYZ\t20.56\tnull\t72.61136209823621\t72.61136209823621\t72.61136209823621\t1970-01-03T11:30:00.000000000Z
                        ABC\t178.3423122144073\t2\t81.46807944500559\t96.87423276940171\t89.17115610720364\t1970-01-03T11:30:00.000000000Z
                        XYZ\t97.55263540567968\t1\t97.55263540567968\t97.55263540567968\t97.55263540567968\t1970-01-03T12:00:00.000000000Z
                        ABC\t20.56\tnull\t81.46807944500559\t96.87423276940171\t89.17115610720364\t1970-01-03T12:00:00.000000000Z
                        XYZ\t135.12700563223447\t2\t37.62501709498378\t97.5019885372507\t67.56350281611724\t1970-01-03T12:30:00.000000000Z
                        ABC\t20.56\tnull\t81.46807944500559\t96.87423276940171\t89.17115610720364\t1970-01-03T12:30:00.000000000Z
                        XYZ\t20.56\tnull\t37.62501709498378\t97.5019885372507\t67.56350281611724\t1970-01-03T13:00:00.000000000Z
                        ABC\t90.75843364017028\t1\t90.75843364017028\t90.75843364017028\t90.75843364017028\t1970-01-03T13:00:00.000000000Z
                        XYZ\t140.19873890621585\t2\t51.824519718206766\t88.37421918800908\t70.09936945310793\t1970-01-03T13:30:00.000000000Z
                        ABC\t20.56\tnull\t90.75843364017028\t90.75843364017028\t90.75843364017028\t1970-01-03T13:30:00.000000000Z
                        XYZ\t20.56\tnull\t51.824519718206766\t88.37421918800908\t70.09936945310793\t1970-01-03T14:00:00.000000000Z
                        ABC\t45.659895188239794\t1\t45.659895188239794\t45.659895188239794\t45.659895188239794\t1970-01-03T14:00:00.000000000Z
                        """,
                "select b, sum(a), count(), min(a), max(a), avg(a), k from x sample by 30m fill(20.56, null, prev, prev, prev)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol('ABC', 'XYZ') b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        " union " +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol('ABC', 'XYZ') b," +
                        " timestamp_sequence_ns(212400000000000, 600000000000) k" +
                        " from" +
                        " long_sequence(10)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillValueListWithNullAndPrevAndLinear() throws Exception {
        assertException(
                "select b, sum(a), count(), min(a), max(a), avg(a), k from x sample by 30m fill(20.56, null, prev, prev, linear)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol('ABC', 'XYZ') b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(10)" +
                        ") timestamp(k) partition by NONE",
                0,
                "linear interpolation is not supported when using fill values for keyed sample by expression"
        );
    }

    @Test
    public void testSampleFillValueListWithNullAndPrevAndLinearAllTypesNotKeyed() throws Exception {
        assertQuery(
                """
                        sum\tcount\tmin\tmax\tsum1\tmax1\tcount1\tsum2\tsum3\tsum4\tk
                        1569490116\t1\tnull\t0.7611029\t428\t2015-05-16T20:27:48.158Z\t1\t-8671107786057422727\t26\t0.15786635599554755\t1970-01-03T00:00:00.000000000Z
                        123\tnull\tnull\tnull\tnull\t2015-05-16T20:27:48.158Z\t1\t556\t28\t0.15786635599554755\t1970-01-03T00:15:00.000000000Z
                        123\tnull\tnull\tnull\tnull\t2015-05-16T20:27:48.158Z\t1\t556\t30\t0.15786635599554755\t1970-01-03T00:30:00.000000000Z
                        123\tnull\tnull\tnull\tnull\t2015-05-16T20:27:48.158Z\t1\t556\t32\t0.15786635599554755\t1970-01-03T00:45:00.000000000Z
                        -2132716300\t1\t0.38179758047769774\tnull\t813\t2015-07-01T22:08:50.655Z\t1\t-6186964045554120476\t34\t0.04142812470232493\t1970-01-03T01:00:00.000000000Z
                        123\tnull\t0.38179758047769774\tnull\tnull\t2015-07-01T22:08:50.655Z\t1\t556\t33\t0.04142812470232493\t1970-01-03T01:15:00.000000000Z
                        123\tnull\t0.38179758047769774\tnull\tnull\t2015-07-01T22:08:50.655Z\t1\t556\t32\t0.04142812470232493\t1970-01-03T01:30:00.000000000Z
                        123\tnull\t0.38179758047769774\tnull\tnull\t2015-07-01T22:08:50.655Z\t1\t556\t31\t0.04142812470232493\t1970-01-03T01:45:00.000000000Z
                        -360860352\t1\t0.456344569609078\tnull\t1013\t2015-01-15T20:11:07.487Z\t1\t5271904137583983788\t30\t0.6752509547112409\t1970-01-03T02:00:00.000000000Z
                        123\tnull\t0.456344569609078\tnull\tnull\t2015-01-15T20:11:07.487Z\t1\t556\t25\t0.6752509547112409\t1970-01-03T02:15:00.000000000Z
                        123\tnull\t0.456344569609078\tnull\tnull\t2015-01-15T20:11:07.487Z\t1\t556\t20\t0.6752509547112409\t1970-01-03T02:30:00.000000000Z
                        123\tnull\t0.456344569609078\tnull\tnull\t2015-01-15T20:11:07.487Z\t1\t556\t15\t0.6752509547112409\t1970-01-03T02:45:00.000000000Z
                        2060263242\t1\tnull\t0.34947264\t869\t2015-05-15T18:43:06.827Z\t1\t-5439556746612026472\t11\tnull\t1970-01-03T03:00:00.000000000Z
                        123\tnull\tnull\t0.28652155\tnull\t2015-05-15T18:43:06.827Z\t1\t556\t16\tnull\t1970-01-03T03:15:00.000000000Z
                        123\tnull\tnull\t0.22357047\tnull\t2015-05-15T18:43:06.827Z\t1\t556\t21\tnull\t1970-01-03T03:30:00.000000000Z
                        123\tnull\tnull\t0.16061938\tnull\t2015-05-15T18:43:06.827Z\t1\t556\t26\tnull\t1970-01-03T03:45:00.000000000Z
                        502711083\t1\t0.0171850098561398\t0.09766829\t605\t2015-07-12T07:33:54.007Z\t1\t-6187389706549636253\t32\t0.22631523434159562\t1970-01-03T04:00:00.000000000Z
                        """,
                "select sum(a),count(),min(d),max(e),sum(f),max(g),count(),sum(j),sum(l),sum(o), k " +
                        "from x sample by 15m fill(123,null,prev,linear,null,prev,prev,556,linear,prev)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_int() a," +
                        " rnd_boolean() b," +
                        " rnd_str(1,1,2) c," +
                        " rnd_double(2) d," +
                        " rnd_float(2) e," +
                        " rnd_short(10,1024) f," +
                        " rnd_date(to_date('2015', 'yyyy'), to_date('2016', 'yyyy'), 2) g," +
                        " rnd_symbol(4,4,4,2) i," +
                        " rnd_long() j," +
                        " rnd_byte(2,50) l," +
                        " rnd_bin(10, 20, 2) m," +
                        " rnd_str(5,16,2) n," +
                        " rnd_double(2) o," +
                        " timestamp_sequence_ns(0, 3600000000000) p," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillValueListWithNullAndPrevAndLinearNotKeyed() throws Exception {
        assertQuery(
                """
                        sum\tcount\tmin\tmax\tk
                        76.75673070796104\t1\t76.75673070796104\t76.75673070796104\t1970-01-03T01:00:00.000000000Z
                        20.56\tnull\t76.75673070796104\t76.75673070796104\t1970-01-03T01:30:00.000000000Z
                        20.56\tnull\t76.75673070796104\t76.75673070796104\t1970-01-03T02:00:00.000000000Z
                        20.56\tnull\t76.75673070796104\t76.75673070796104\t1970-01-03T02:30:00.000000000Z
                        63.81607531178513\t1\t63.81607531178513\t63.81607531178513\t1970-01-03T03:00:00.000000000Z
                        20.56\tnull\t63.81607531178513\t63.81607531178513\t1970-01-03T03:30:00.000000000Z
                        20.56\tnull\t63.81607531178513\t63.81607531178513\t1970-01-03T04:00:00.000000000Z
                        20.56\tnull\t63.81607531178513\t63.81607531178513\t1970-01-03T04:30:00.000000000Z
                        12.026122412833129\t1\t12.026122412833129\t12.026122412833129\t1970-01-03T05:00:00.000000000Z
                        20.56\tnull\t12.026122412833129\t12.026122412833129\t1970-01-03T05:30:00.000000000Z
                        20.56\tnull\t12.026122412833129\t12.026122412833129\t1970-01-03T06:00:00.000000000Z
                        20.56\tnull\t12.026122412833129\t12.026122412833129\t1970-01-03T06:30:00.000000000Z
                        26.922103479744898\t1\t26.922103479744898\t26.922103479744898\t1970-01-03T07:00:00.000000000Z
                        20.56\tnull\t26.922103479744898\t26.922103479744898\t1970-01-03T07:30:00.000000000Z
                        20.56\tnull\t26.922103479744898\t26.922103479744898\t1970-01-03T08:00:00.000000000Z
                        20.56\tnull\t26.922103479744898\t26.922103479744898\t1970-01-03T08:30:00.000000000Z
                        84.45258177211063\t1\t84.45258177211063\t84.45258177211063\t1970-01-03T09:00:00.000000000Z
                        20.56\tnull\t84.45258177211063\t84.45258177211063\t1970-01-03T09:30:00.000000000Z
                        20.56\tnull\t84.45258177211063\t84.45258177211063\t1970-01-03T10:00:00.000000000Z
                        20.56\tnull\t84.45258177211063\t84.45258177211063\t1970-01-03T10:30:00.000000000Z
                        49.00510449885239\t1\t49.00510449885239\t49.00510449885239\t1970-01-03T11:00:00.000000000Z
                        132.50550921779725\t2\t40.455469747939254\t92.050039469858\t1970-01-03T11:30:00.000000000Z
                        9.750574414434398\t1\t9.750574414434398\t9.750574414434398\t1970-01-03T12:00:00.000000000Z
                        69.81364002755922\t2\t12.02416087573498\t57.78947915182423\t1970-01-03T12:30:00.000000000Z
                        58.912164838797885\t1\t58.912164838797885\t58.912164838797885\t1970-01-03T13:00:00.000000000Z
                        139.22127942393962\t2\t44.80468966861358\t94.41658975532606\t1970-01-03T13:30:00.000000000Z
                        94.55893004802432\t1\t94.55893004802432\t94.55893004802432\t1970-01-03T14:00:00.000000000Z
                        """,
                "select sum(a), count(), min(a), max(a), k from x sample by 30m fill(20.56, null, prev, prev, linear)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(5)" +
                        " union " +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(212400000000000, 600000000000) k" +
                        " from" +
                        " long_sequence(10)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSampleFillValueNotEnough() throws Exception {
        assertException(
                "select b, sum(a), sum(c), sum(d), sum(e), sum(f), sum(g), k from x sample by 3h fill(20.56, 0, 0, 0, 0)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " rnd_float(0)*100 c," +
                        " abs(rnd_int()) d," +
                        " rnd_short() e," +
                        " rnd_byte(3,10) f," +
                        " rnd_long() g," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(20)" +
                        ") timestamp(k) partition by NONE",
                101,
                "insufficient fill values for SAMPLE BY FILL"
        );
    }

    @Test
    public void testSampleFillValueNotKeyed() throws Exception {
        assertQuery(
                """
                        sum\tk
                        11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        20.56\t1970-01-03T00:30:00.000000000Z
                        42.17768841969397\t1970-01-03T01:00:00.000000000Z
                        20.56\t1970-01-03T01:30:00.000000000Z
                        23.90529010846525\t1970-01-03T02:00:00.000000000Z
                        20.56\t1970-01-03T02:30:00.000000000Z
                        70.94360487171201\t1970-01-03T03:00:00.000000000Z
                        20.56\t1970-01-03T03:30:00.000000000Z
                        87.99634725391621\t1970-01-03T04:00:00.000000000Z
                        20.56\t1970-01-03T04:30:00.000000000Z
                        32.881769076795045\t1970-01-03T05:00:00.000000000Z
                        20.56\t1970-01-03T05:30:00.000000000Z
                        97.71103146051203\t1970-01-03T06:00:00.000000000Z
                        20.56\t1970-01-03T06:30:00.000000000Z
                        81.46807944500559\t1970-01-03T07:00:00.000000000Z
                        20.56\t1970-01-03T07:30:00.000000000Z
                        57.93466326862211\t1970-01-03T08:00:00.000000000Z
                        20.56\t1970-01-03T08:30:00.000000000Z
                        12.026122412833129\t1970-01-03T09:00:00.000000000Z
                        """,
                "select sum(a), k from x sample by 30m fill(20.56)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(10)" +
                        ") timestamp(k) partition by NONE",
                "k",
                true
        );
    }

    @Test
    public void testSampleFillValueNotKeyedAlignToCalendar() throws Exception {
        assertQuery(
                """
                        sum\tk
                        11.427984775756228\t1970-01-03T00:00:00.000000000Z
                        42.17768841969397\t1970-01-03T00:30:00.000000000Z
                        20.56\t1970-01-03T01:00:00.000000000Z
                        23.90529010846525\t1970-01-03T01:30:00.000000000Z
                        20.56\t1970-01-03T02:00:00.000000000Z
                        70.94360487171201\t1970-01-03T02:30:00.000000000Z
                        20.56\t1970-01-03T03:00:00.000000000Z
                        87.99634725391621\t1970-01-03T03:30:00.000000000Z
                        20.56\t1970-01-03T04:00:00.000000000Z
                        32.881769076795045\t1970-01-03T04:30:00.000000000Z
                        20.56\t1970-01-03T05:00:00.000000000Z
                        97.71103146051203\t1970-01-03T05:30:00.000000000Z
                        20.56\t1970-01-03T06:00:00.000000000Z
                        81.46807944500559\t1970-01-03T06:30:00.000000000Z
                        20.56\t1970-01-03T07:00:00.000000000Z
                        57.93466326862211\t1970-01-03T07:30:00.000000000Z
                        20.56\t1970-01-03T08:00:00.000000000Z
                        12.026122412833129\t1970-01-03T08:30:00.000000000Z
                        48.820511018586934\t1970-01-03T09:00:00.000000000Z
                        20.56\t1970-01-03T09:30:00.000000000Z
                        26.922103479744898\t1970-01-03T10:00:00.000000000Z
                        20.56\t1970-01-03T10:30:00.000000000Z
                        52.98405941762054\t1970-01-03T11:00:00.000000000Z
                        20.56\t1970-01-03T11:30:00.000000000Z
                        84.45258177211063\t1970-01-03T12:00:00.000000000Z
                        20.56\t1970-01-03T12:30:00.000000000Z
                        97.5019885372507\t1970-01-03T13:00:00.000000000Z
                        20.56\t1970-01-03T13:30:00.000000000Z
                        49.00510449885239\t1970-01-03T14:00:00.000000000Z
                        20.56\t1970-01-03T14:30:00.000000000Z
                        80.01121139739173\t1970-01-03T15:00:00.000000000Z
                        20.56\t1970-01-03T15:30:00.000000000Z
                        92.050039469858\t1970-01-03T16:00:00.000000000Z
                        20.56\t1970-01-03T16:30:00.000000000Z
                        45.6344569609078\t1970-01-03T17:00:00.000000000Z
                        40.455469747939254\t1970-01-03T17:30:00.000000000Z
                        20.56\t1970-01-03T18:00:00.000000000Z
                        56.594291398612405\t1970-01-03T18:30:00.000000000Z
                        20.56\t1970-01-03T19:00:00.000000000Z
                        9.750574414434398\t1970-01-03T19:30:00.000000000Z
                        20.56\t1970-01-03T20:00:00.000000000Z
                        12.105630273556178\t1970-01-03T20:30:00.000000000Z
                        20.56\t1970-01-03T21:00:00.000000000Z
                        57.78947915182423\t1970-01-03T21:30:00.000000000Z
                        20.56\t1970-01-03T22:00:00.000000000Z
                        86.85154305419587\t1970-01-03T22:30:00.000000000Z
                        20.56\t1970-01-03T23:00:00.000000000Z
                        12.02416087573498\t1970-01-03T23:30:00.000000000Z
                        20.56\t1970-01-04T00:00:00.000000000Z
                        49.42890511958454\t1970-01-04T00:30:00.000000000Z
                        20.56\t1970-01-04T01:00:00.000000000Z
                        58.912164838797885\t1970-01-04T01:30:00.000000000Z
                        67.52509547112409\t1970-01-04T02:00:00.000000000Z
                        20.56\t1970-01-04T02:30:00.000000000Z
                        44.80468966861358\t1970-01-04T03:00:00.000000000Z
                        20.56\t1970-01-04T03:30:00.000000000Z
                        89.40917126581896\t1970-01-04T04:00:00.000000000Z
                        20.56\t1970-01-04T04:30:00.000000000Z
                        94.41658975532606\t1970-01-04T05:00:00.000000000Z
                        20.56\t1970-01-04T05:30:00.000000000Z
                        62.5966045857722\t1970-01-04T06:00:00.000000000Z
                        20.56\t1970-01-04T06:30:00.000000000Z
                        94.55893004802432\t1970-01-04T07:00:00.000000000Z
                        20.56\t1970-01-04T07:30:00.000000000Z
                        21.85865835029681\t1970-01-04T08:00:00.000000000Z
                        20.56\t1970-01-04T08:30:00.000000000Z
                        3.993124821273464\t1970-01-04T09:00:00.000000000Z
                        20.56\t1970-01-04T09:30:00.000000000Z
                        84.3845956391477\t1970-01-04T10:00:00.000000000Z
                        48.92743433711657\t1970-01-04T10:30:00.000000000Z
                        20.56\t1970-01-04T11:00:00.000000000Z
                        66.97969295620055\t1970-01-04T11:30:00.000000000Z
                        20.56\t1970-01-04T12:00:00.000000000Z
                        58.93398488053903\t1970-01-04T12:30:00.000000000Z
                        """,
                "select sum(a), k from x sample by 30m fill(20.56)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172866000000000, 3400000000000) k" +
                        " from" +
                        " long_sequence(40)" +
                        ") timestamp(k) partition by NONE",
                "k",
                true
        );
    }

    @Test
    public void testSampleFillValueNotKeyedAlignToCalendarOffset() throws Exception {
        // this test verifies transition from Summer to Winter time and
        // clock going backwards. An hour of time should drop out of the result set
        // without the logic trying to back fill things
        assertQuery(
                """
                        sum\tk
                        11.427984775756228\t2021-10-30T23:40:00.000000000Z
                        20.56\t2021-10-31T00:10:00.000000000Z
                        42.17768841969397\t2021-10-31T00:40:00.000000000Z
                        20.56\t2021-10-31T01:10:00.000000000Z
                        23.90529010846525\t2021-10-31T01:40:00.000000000Z
                        20.56\t2021-10-31T02:10:00.000000000Z
                        70.94360487171201\t2021-10-31T02:40:00.000000000Z
                        20.56\t2021-10-31T03:10:00.000000000Z
                        87.99634725391621\t2021-10-31T03:40:00.000000000Z
                        20.56\t2021-10-31T04:10:00.000000000Z
                        32.881769076795045\t2021-10-31T04:40:00.000000000Z
                        20.56\t2021-10-31T05:10:00.000000000Z
                        97.71103146051203\t2021-10-31T05:40:00.000000000Z
                        81.46807944500559\t2021-10-31T06:10:00.000000000Z
                        20.56\t2021-10-31T06:40:00.000000000Z
                        57.93466326862211\t2021-10-31T07:10:00.000000000Z
                        20.56\t2021-10-31T07:40:00.000000000Z
                        12.026122412833129\t2021-10-31T08:10:00.000000000Z
                        20.56\t2021-10-31T08:40:00.000000000Z
                        48.820511018586934\t2021-10-31T09:10:00.000000000Z
                        20.56\t2021-10-31T09:40:00.000000000Z
                        26.922103479744898\t2021-10-31T10:10:00.000000000Z
                        20.56\t2021-10-31T10:40:00.000000000Z
                        52.98405941762054\t2021-10-31T11:10:00.000000000Z
                        20.56\t2021-10-31T11:40:00.000000000Z
                        84.45258177211063\t2021-10-31T12:10:00.000000000Z
                        20.56\t2021-10-31T12:40:00.000000000Z
                        97.5019885372507\t2021-10-31T13:10:00.000000000Z
                        20.56\t2021-10-31T13:40:00.000000000Z
                        49.00510449885239\t2021-10-31T14:10:00.000000000Z
                        80.01121139739173\t2021-10-31T14:40:00.000000000Z
                        20.56\t2021-10-31T15:10:00.000000000Z
                        92.050039469858\t2021-10-31T15:40:00.000000000Z
                        20.56\t2021-10-31T16:10:00.000000000Z
                        45.6344569609078\t2021-10-31T16:40:00.000000000Z
                        20.56\t2021-10-31T17:10:00.000000000Z
                        40.455469747939254\t2021-10-31T17:40:00.000000000Z
                        20.56\t2021-10-31T18:10:00.000000000Z
                        56.594291398612405\t2021-10-31T18:40:00.000000000Z
                        20.56\t2021-10-31T19:10:00.000000000Z
                        9.750574414434398\t2021-10-31T19:40:00.000000000Z
                        20.56\t2021-10-31T20:10:00.000000000Z
                        12.105630273556178\t2021-10-31T20:40:00.000000000Z
                        20.56\t2021-10-31T21:10:00.000000000Z
                        57.78947915182423\t2021-10-31T21:40:00.000000000Z
                        20.56\t2021-10-31T22:10:00.000000000Z
                        86.85154305419587\t2021-10-31T22:40:00.000000000Z
                        12.02416087573498\t2021-10-31T23:10:00.000000000Z
                        20.56\t2021-10-31T23:40:00.000000000Z
                        49.42890511958454\t2021-11-01T00:10:00.000000000Z
                        20.56\t2021-11-01T00:40:00.000000000Z
                        58.912164838797885\t2021-11-01T01:10:00.000000000Z
                        20.56\t2021-11-01T01:40:00.000000000Z
                        67.52509547112409\t2021-11-01T02:10:00.000000000Z
                        20.56\t2021-11-01T02:40:00.000000000Z
                        44.80468966861358\t2021-11-01T03:10:00.000000000Z
                        20.56\t2021-11-01T03:40:00.000000000Z
                        89.40917126581896\t2021-11-01T04:10:00.000000000Z
                        20.56\t2021-11-01T04:40:00.000000000Z
                        94.41658975532606\t2021-11-01T05:10:00.000000000Z
                        20.56\t2021-11-01T05:40:00.000000000Z
                        62.5966045857722\t2021-11-01T06:10:00.000000000Z
                        20.56\t2021-11-01T06:40:00.000000000Z
                        94.55893004802432\t2021-11-01T07:10:00.000000000Z
                        21.85865835029681\t2021-11-01T07:40:00.000000000Z
                        20.56\t2021-11-01T08:10:00.000000000Z
                        3.993124821273464\t2021-11-01T08:40:00.000000000Z
                        20.56\t2021-11-01T09:10:00.000000000Z
                        84.3845956391477\t2021-11-01T09:40:00.000000000Z
                        20.56\t2021-11-01T10:10:00.000000000Z
                        48.92743433711657\t2021-11-01T10:40:00.000000000Z
                        20.56\t2021-11-01T11:10:00.000000000Z
                        66.97969295620055\t2021-11-01T11:40:00.000000000Z
                        20.56\t2021-11-01T12:10:00.000000000Z
                        58.93398488053903\t2021-11-01T12:40:00.000000000Z
                        """,
                "select sum(a), k from x sample by 30m fill(20.56) align to calendar with offset '00:40'",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-10-31T00:00:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(40)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false,
                false
        );
    }

    @Test
    public void testSampleFillValueNotKeyedAlignToCalendarTimeZone() throws Exception {
        assertQuery(
                """
                        sum\tk
                        11.427984775756228\t2021-03-28T00:00:00.000000000Z
                        42.17768841969397\t2021-03-28T00:30:00.000000000Z
                        20.56\t2021-03-28T01:00:00.000000000Z
                        23.90529010846525\t2021-03-28T01:30:00.000000000Z
                        20.56\t2021-03-28T02:00:00.000000000Z
                        70.94360487171201\t2021-03-28T02:30:00.000000000Z
                        20.56\t2021-03-28T03:00:00.000000000Z
                        87.99634725391621\t2021-03-28T03:30:00.000000000Z
                        20.56\t2021-03-28T04:00:00.000000000Z
                        32.881769076795045\t2021-03-28T04:30:00.000000000Z
                        20.56\t2021-03-28T05:00:00.000000000Z
                        97.71103146051203\t2021-03-28T05:30:00.000000000Z
                        20.56\t2021-03-28T06:00:00.000000000Z
                        81.46807944500559\t2021-03-28T06:30:00.000000000Z
                        20.56\t2021-03-28T07:00:00.000000000Z
                        57.93466326862211\t2021-03-28T07:30:00.000000000Z
                        20.56\t2021-03-28T08:00:00.000000000Z
                        12.026122412833129\t2021-03-28T08:30:00.000000000Z
                        48.820511018586934\t2021-03-28T09:00:00.000000000Z
                        20.56\t2021-03-28T09:30:00.000000000Z
                        26.922103479744898\t2021-03-28T10:00:00.000000000Z
                        20.56\t2021-03-28T10:30:00.000000000Z
                        52.98405941762054\t2021-03-28T11:00:00.000000000Z
                        20.56\t2021-03-28T11:30:00.000000000Z
                        84.45258177211063\t2021-03-28T12:00:00.000000000Z
                        20.56\t2021-03-28T12:30:00.000000000Z
                        97.5019885372507\t2021-03-28T13:00:00.000000000Z
                        20.56\t2021-03-28T13:30:00.000000000Z
                        49.00510449885239\t2021-03-28T14:00:00.000000000Z
                        20.56\t2021-03-28T14:30:00.000000000Z
                        80.01121139739173\t2021-03-28T15:00:00.000000000Z
                        20.56\t2021-03-28T15:30:00.000000000Z
                        92.050039469858\t2021-03-28T16:00:00.000000000Z
                        20.56\t2021-03-28T16:30:00.000000000Z
                        45.6344569609078\t2021-03-28T17:00:00.000000000Z
                        40.455469747939254\t2021-03-28T17:30:00.000000000Z
                        20.56\t2021-03-28T18:00:00.000000000Z
                        56.594291398612405\t2021-03-28T18:30:00.000000000Z
                        20.56\t2021-03-28T19:00:00.000000000Z
                        9.750574414434398\t2021-03-28T19:30:00.000000000Z
                        20.56\t2021-03-28T20:00:00.000000000Z
                        12.105630273556178\t2021-03-28T20:30:00.000000000Z
                        20.56\t2021-03-28T21:00:00.000000000Z
                        57.78947915182423\t2021-03-28T21:30:00.000000000Z
                        20.56\t2021-03-28T22:00:00.000000000Z
                        86.85154305419587\t2021-03-28T22:30:00.000000000Z
                        20.56\t2021-03-28T23:00:00.000000000Z
                        12.02416087573498\t2021-03-28T23:30:00.000000000Z
                        20.56\t2021-03-29T00:00:00.000000000Z
                        49.42890511958454\t2021-03-29T00:30:00.000000000Z
                        20.56\t2021-03-29T01:00:00.000000000Z
                        58.912164838797885\t2021-03-29T01:30:00.000000000Z
                        67.52509547112409\t2021-03-29T02:00:00.000000000Z
                        20.56\t2021-03-29T02:30:00.000000000Z
                        44.80468966861358\t2021-03-29T03:00:00.000000000Z
                        20.56\t2021-03-29T03:30:00.000000000Z
                        89.40917126581896\t2021-03-29T04:00:00.000000000Z
                        20.56\t2021-03-29T04:30:00.000000000Z
                        94.41658975532606\t2021-03-29T05:00:00.000000000Z
                        20.56\t2021-03-29T05:30:00.000000000Z
                        62.5966045857722\t2021-03-29T06:00:00.000000000Z
                        20.56\t2021-03-29T06:30:00.000000000Z
                        94.55893004802432\t2021-03-29T07:00:00.000000000Z
                        20.56\t2021-03-29T07:30:00.000000000Z
                        21.85865835029681\t2021-03-29T08:00:00.000000000Z
                        20.56\t2021-03-29T08:30:00.000000000Z
                        3.993124821273464\t2021-03-29T09:00:00.000000000Z
                        20.56\t2021-03-29T09:30:00.000000000Z
                        84.3845956391477\t2021-03-29T10:00:00.000000000Z
                        48.92743433711657\t2021-03-29T10:30:00.000000000Z
                        20.56\t2021-03-29T11:00:00.000000000Z
                        66.97969295620055\t2021-03-29T11:30:00.000000000Z
                        20.56\t2021-03-29T12:00:00.000000000Z
                        58.93398488053903\t2021-03-29T12:30:00.000000000Z
                        """,
                "select sum(a), k from x sample by 30m fill(20.56) align to calendar time zone 'Europe/Berlin'",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-03-28T00:00:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(40)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false,
                false
        );
    }

    @Test
    public void testSampleFillValueNotKeyedAlignToCalendarTimeZone2() throws Exception {
        // this test verifies transition from Summer to Winter time and
        // clock going backwards. An hour of time should drop out of the result set
        // without the logic trying to back fill things
        assertQuery(
                """
                        sum\tk
                        11.427984775756228\t2021-10-31T00:00:00.000000000Z
                        66.08297852815922\t2021-10-31T00:30:00.000000000Z
                        70.94360487171201\t2021-10-31T02:30:00.000000000Z
                        20.56\t2021-10-31T03:00:00.000000000Z
                        87.99634725391621\t2021-10-31T03:30:00.000000000Z
                        20.56\t2021-10-31T04:00:00.000000000Z
                        32.881769076795045\t2021-10-31T04:30:00.000000000Z
                        20.56\t2021-10-31T05:00:00.000000000Z
                        97.71103146051203\t2021-10-31T05:30:00.000000000Z
                        20.56\t2021-10-31T06:00:00.000000000Z
                        81.46807944500559\t2021-10-31T06:30:00.000000000Z
                        20.56\t2021-10-31T07:00:00.000000000Z
                        57.93466326862211\t2021-10-31T07:30:00.000000000Z
                        20.56\t2021-10-31T08:00:00.000000000Z
                        12.026122412833129\t2021-10-31T08:30:00.000000000Z
                        48.820511018586934\t2021-10-31T09:00:00.000000000Z
                        20.56\t2021-10-31T09:30:00.000000000Z
                        26.922103479744898\t2021-10-31T10:00:00.000000000Z
                        20.56\t2021-10-31T10:30:00.000000000Z
                        52.98405941762054\t2021-10-31T11:00:00.000000000Z
                        20.56\t2021-10-31T11:30:00.000000000Z
                        84.45258177211063\t2021-10-31T12:00:00.000000000Z
                        20.56\t2021-10-31T12:30:00.000000000Z
                        97.5019885372507\t2021-10-31T13:00:00.000000000Z
                        20.56\t2021-10-31T13:30:00.000000000Z
                        49.00510449885239\t2021-10-31T14:00:00.000000000Z
                        20.56\t2021-10-31T14:30:00.000000000Z
                        80.01121139739173\t2021-10-31T15:00:00.000000000Z
                        20.56\t2021-10-31T15:30:00.000000000Z
                        92.050039469858\t2021-10-31T16:00:00.000000000Z
                        20.56\t2021-10-31T16:30:00.000000000Z
                        45.6344569609078\t2021-10-31T17:00:00.000000000Z
                        40.455469747939254\t2021-10-31T17:30:00.000000000Z
                        20.56\t2021-10-31T18:00:00.000000000Z
                        56.594291398612405\t2021-10-31T18:30:00.000000000Z
                        20.56\t2021-10-31T19:00:00.000000000Z
                        9.750574414434398\t2021-10-31T19:30:00.000000000Z
                        20.56\t2021-10-31T20:00:00.000000000Z
                        12.105630273556178\t2021-10-31T20:30:00.000000000Z
                        20.56\t2021-10-31T21:00:00.000000000Z
                        57.78947915182423\t2021-10-31T21:30:00.000000000Z
                        20.56\t2021-10-31T22:00:00.000000000Z
                        86.85154305419587\t2021-10-31T22:30:00.000000000Z
                        20.56\t2021-10-31T23:00:00.000000000Z
                        12.02416087573498\t2021-10-31T23:30:00.000000000Z
                        20.56\t2021-11-01T00:00:00.000000000Z
                        49.42890511958454\t2021-11-01T00:30:00.000000000Z
                        20.56\t2021-11-01T01:00:00.000000000Z
                        58.912164838797885\t2021-11-01T01:30:00.000000000Z
                        67.52509547112409\t2021-11-01T02:00:00.000000000Z
                        20.56\t2021-11-01T02:30:00.000000000Z
                        44.80468966861358\t2021-11-01T03:00:00.000000000Z
                        20.56\t2021-11-01T03:30:00.000000000Z
                        89.40917126581896\t2021-11-01T04:00:00.000000000Z
                        20.56\t2021-11-01T04:30:00.000000000Z
                        94.41658975532606\t2021-11-01T05:00:00.000000000Z
                        20.56\t2021-11-01T05:30:00.000000000Z
                        62.5966045857722\t2021-11-01T06:00:00.000000000Z
                        20.56\t2021-11-01T06:30:00.000000000Z
                        94.55893004802432\t2021-11-01T07:00:00.000000000Z
                        20.56\t2021-11-01T07:30:00.000000000Z
                        21.85865835029681\t2021-11-01T08:00:00.000000000Z
                        20.56\t2021-11-01T08:30:00.000000000Z
                        3.993124821273464\t2021-11-01T09:00:00.000000000Z
                        20.56\t2021-11-01T09:30:00.000000000Z
                        84.3845956391477\t2021-11-01T10:00:00.000000000Z
                        48.92743433711657\t2021-11-01T10:30:00.000000000Z
                        20.56\t2021-11-01T11:00:00.000000000Z
                        66.97969295620055\t2021-11-01T11:30:00.000000000Z
                        20.56\t2021-11-01T12:00:00.000000000Z
                        58.93398488053903\t2021-11-01T12:30:00.000000000Z
                        """,
                "select sum(a), k from x sample by 30m fill(20.56) align to calendar time zone 'Europe/Berlin'",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-10-31T00:00:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(40)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false,
                false
        );
    }

    @Test
    public void testSampleFillValueNotKeyedAlignToCalendarTimeZoneOffset() throws Exception {
        // this test verifies transition from Summer to Winter time and
        // clock going backwards. An hour of time should drop out of the result set
        // without the logic trying to back fill things
        assertQuery(
                """
                        s\tkz
                        2\t2021-10-31T03:10:00.000000000Z
                        1\t2021-10-31T04:10:00.000000000Z
                        9999\t2021-10-31T04:40:00.000000000Z
                        1\t2021-10-31T05:10:00.000000000Z
                        1\t2021-10-31T05:40:00.000000000Z
                        9999\t2021-10-31T06:10:00.000000000Z
                        1\t2021-10-31T06:40:00.000000000Z
                        9999\t2021-10-31T07:10:00.000000000Z
                        1\t2021-10-31T07:40:00.000000000Z
                        9999\t2021-10-31T08:10:00.000000000Z
                        1\t2021-10-31T08:40:00.000000000Z
                        9999\t2021-10-31T09:10:00.000000000Z
                        1\t2021-10-31T09:40:00.000000000Z
                        9999\t2021-10-31T10:10:00.000000000Z
                        1\t2021-10-31T10:40:00.000000000Z
                        9999\t2021-10-31T11:10:00.000000000Z
                        1\t2021-10-31T11:40:00.000000000Z
                        9999\t2021-10-31T12:10:00.000000000Z
                        1\t2021-10-31T12:40:00.000000000Z
                        9999\t2021-10-31T13:10:00.000000000Z
                        1\t2021-10-31T13:40:00.000000000Z
                        1\t2021-10-31T14:10:00.000000000Z
                        9999\t2021-10-31T14:40:00.000000000Z
                        1\t2021-10-31T15:10:00.000000000Z
                        9999\t2021-10-31T15:40:00.000000000Z
                        1\t2021-10-31T16:10:00.000000000Z
                        9999\t2021-10-31T16:40:00.000000000Z
                        1\t2021-10-31T17:10:00.000000000Z
                        9999\t2021-10-31T17:40:00.000000000Z
                        1\t2021-10-31T18:10:00.000000000Z
                        9999\t2021-10-31T18:40:00.000000000Z
                        1\t2021-10-31T19:10:00.000000000Z
                        9999\t2021-10-31T19:40:00.000000000Z
                        1\t2021-10-31T20:10:00.000000000Z
                        9999\t2021-10-31T20:40:00.000000000Z
                        1\t2021-10-31T21:10:00.000000000Z
                        9999\t2021-10-31T21:40:00.000000000Z
                        1\t2021-10-31T22:10:00.000000000Z
                        1\t2021-10-31T22:40:00.000000000Z
                        9999\t2021-10-31T23:10:00.000000000Z
                        1\t2021-10-31T23:40:00.000000000Z
                        9999\t2021-11-01T00:10:00.000000000Z
                        1\t2021-11-01T00:40:00.000000000Z
                        9999\t2021-11-01T01:10:00.000000000Z
                        1\t2021-11-01T01:40:00.000000000Z
                        9999\t2021-11-01T02:10:00.000000000Z
                        1\t2021-11-01T02:40:00.000000000Z
                        9999\t2021-11-01T03:10:00.000000000Z
                        1\t2021-11-01T03:40:00.000000000Z
                        9999\t2021-11-01T04:10:00.000000000Z
                        1\t2021-11-01T04:40:00.000000000Z
                        9999\t2021-11-01T05:10:00.000000000Z
                        1\t2021-11-01T05:40:00.000000000Z
                        9999\t2021-11-01T06:10:00.000000000Z
                        1\t2021-11-01T06:40:00.000000000Z
                        1\t2021-11-01T07:10:00.000000000Z
                        9999\t2021-11-01T07:40:00.000000000Z
                        1\t2021-11-01T08:10:00.000000000Z
                        9999\t2021-11-01T08:40:00.000000000Z
                        1\t2021-11-01T09:10:00.000000000Z
                        9999\t2021-11-01T09:40:00.000000000Z
                        1\t2021-11-01T10:10:00.000000000Z
                        9999\t2021-11-01T10:40:00.000000000Z
                        1\t2021-11-01T11:10:00.000000000Z
                        9999\t2021-11-01T11:40:00.000000000Z
                        1\t2021-11-01T12:10:00.000000000Z
                        9999\t2021-11-01T12:40:00.000000000Z
                        1\t2021-11-01T13:10:00.000000000Z
                        9999\t2021-11-01T13:40:00.000000000Z
                        1\t2021-11-01T14:10:00.000000000Z
                        9999\t2021-11-01T14:40:00.000000000Z
                        1\t2021-11-01T15:10:00.000000000Z
                        """,
                "select s, to_timezone(k, 'Europe/Riga') kz from (select count() s, k from x sample by 30m fill(9999) align to calendar time zone 'Europe/Riga' with offset '00:40')",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-10-31T00:22:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(40)" +
                        ") timestamp(k) partition by NONE",
                null,
                false
        );
    }

    @Test
    public void testSampleFillValueNotKeyedEmpty() throws Exception {
        assertQuery(
                "sum\tk\n",
                "select sum(a), k from x sample by 30m fill(20.56) align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(0)" +
                        ") timestamp(k) partition by NONE",
                "k",
                "insert into x select * from " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence(172866000000, 3400000000) k" +
                        " from" +
                        " long_sequence(10)" +
                        ") timestamp(k)",
                """
                        sum\tk
                        0.35983672154330515\t1970-01-03T00:01:06.000000000Z
                        76.75673070796104\t1970-01-03T00:31:06.000000000Z
                        20.56\t1970-01-03T01:01:06.000000000Z
                        62.173267078530984\t1970-01-03T01:31:06.000000000Z
                        20.56\t1970-01-03T02:01:06.000000000Z
                        63.81607531178513\t1970-01-03T02:31:06.000000000Z
                        20.56\t1970-01-03T03:01:06.000000000Z
                        57.93466326862211\t1970-01-03T03:31:06.000000000Z
                        20.56\t1970-01-03T04:01:06.000000000Z
                        12.026122412833129\t1970-01-03T04:31:06.000000000Z
                        20.56\t1970-01-03T05:01:06.000000000Z
                        48.820511018586934\t1970-01-03T05:31:06.000000000Z
                        20.56\t1970-01-03T06:01:06.000000000Z
                        26.922103479744898\t1970-01-03T06:31:06.000000000Z
                        20.56\t1970-01-03T07:01:06.000000000Z
                        52.98405941762054\t1970-01-03T07:31:06.000000000Z
                        20.56\t1970-01-03T08:01:06.000000000Z
                        84.45258177211063\t1970-01-03T08:31:06.000000000Z
                        """,
                false
        );
    }

    @Test
    public void testSampleFillValueNotKeyedInvalid() throws Exception {
        assertException(
                "select sum(a), k from x sample by 30m fill(zz)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-03-28T00:00:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(40)" +
                        ") timestamp(k) partition by NONE",
                43,
                "Invalid column: zz"
        );
    }

    @Test
    public void testSampleFillValueNotKeyedInvalidSequential() throws Exception {
        assertException(
                "select sum(a), k from x sample by 30m fill(zz) align to calendar with offset '10:00'",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(cast('2021-03-28T00:00:00.000000000Z' as timestamp_ns), 3400000000000) k" +
                        " from" +
                        " long_sequence(40)" +
                        ") timestamp(k) partition by NONE",
                43,
                "invalid fill value: zz"
        );
    }

    @Test
    public void testSampleFillWithWeekStride() throws Exception {
        assertMemoryLeak(() -> {
            execute(FROM_TO_DDL);
            String query1 = "select ts, avg(x) from fromto\n" +
                    "sample by 1w from '2017-12-20' to '2018-01-31' fill(null)";
            String expected1 = """
                    ts\tavg
                    2017-12-20T00:00:00.000000000Z\tnull
                    2017-12-27T00:00:00.000000000Z\t48.5
                    2018-01-03T00:00:00.000000000Z\t264.5
                    2018-01-10T00:00:00.000000000Z\t456.5
                    2018-01-17T00:00:00.000000000Z\tnull
                    2018-01-24T00:00:00.000000000Z\tnull
                    """;
            assertSql(expected1, query1);

            String query2 = "select ts, avg(x) from fromto\n" +
                    "sample by 1w fill(null)";
            assertQueryNoLeakCheck(
                    """
                            ts\tavg
                            2018-01-01T00:00:00.000000000Z\t168.5
                            2018-01-08T00:00:00.000000000Z\t408.5
                            """,
                    query2,
                    "ts",
                    true
            );

            String query3 = query1.replace("1w", "2w");
            assertQueryNoLeakCheck(
                    """
                            ts\tavg
                            2017-12-20T00:00:00.000000000Z\t48.5
                            2018-01-03T00:00:00.000000000Z\t288.5
                            2018-01-17T00:00:00.000000000Z\tnull
                            """,
                    query3,
                    "ts",
                    true
            );
        });
    }

    @Test
    public void testSamplePeriodInvalidWithNoUnits() throws Exception {
        testSampleByPeriodFails(
                "select sum(a), k from x sample by 300/10 align to calendar",
                "select sum(a), k from x sample by 300/10 a".length() - 1,
                "one letter sample by period unit expected"
        );
    }

    @Test
    public void testSamplePeriodInvalidWithNoUnits2() throws Exception {
        testSampleByPeriodFails(
                "select sum(a), k from x sample by 300/10",
                "select sum(a), k from x sample by 300/10".length(),
                "time interval unit expected"
        );
    }

    @Test
    public void testSamplePeriodInvalidWithWrongUnit() throws Exception {
        testSampleByPeriodFails(
                "select sum(a), k from x sample by 300/10 milli",
                "select sum(a), k from x sample by 300/10 m".length() - 1,
                "one letter sample by period unit expected"
        );
    }

    @Test
    public void testSamplePeriodInvalidWithWrongUnitLetter() throws Exception {
        testSampleByPeriodFails(
                "select sum(a), k from x sample by 300/10 L",
                "select sum(a), k from x sample by 300/10 L".length() - 1,
                "one letter sample by period unit expected"
        );
    }

    @Test
    public void testSimpleArithmeticsInPeriod() throws Exception {
        assertQuery(
                "sum\tk\n",
                "select sum(a), k from x sample by (10+20)m",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(0)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSimpleArithmeticsInPeriod2() throws Exception {
        assertQuery(
                """
                        sum\tk
                        1592.0966416600525\t1970-01-03T00:00:00.000000000Z
                        1566.8131178120786\t1970-01-04T06:00:00.000000000Z
                        1393.2872924527742\t1970-01-05T12:00:00.000000000Z
                        584.4161505427071\t1970-01-06T18:00:00.000000000Z
                        """,
                "select sum(a), k from x sample by (10+20) h align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSimpleArithmeticsInPeriod3() throws Exception {
        assertQuery(
                """
                        sum\tk
                        1592.0966416600525\t1970-01-03T00:00:00.000000000Z
                        1566.8131178120786\t1970-01-04T06:00:00.000000000Z
                        1393.2872924527742\t1970-01-05T12:00:00.000000000Z
                        584.4161505427071\t1970-01-06T18:00:00.000000000Z
                        """,
                "select sum(a), k from x sample by 300/10 h align to first observation",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(100)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testSimpleLongArithmeticsInPeriod() throws Exception {
        assertQuery(
                "sum\tk\n",
                "select sum(a), k from x sample by (1+2)*10L m align to calendar",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_double(0)*100 a," +
                        " rnd_symbol(5,4,4,1) b," +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(0)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testTimestampColumnAliasPosFirst() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table ap_systems as (select timestamp_sequence_ns(0, 60 * 1000000000) ts, rnd_double() hourly_production from long_sequence(100)) timestamp(ts) partition by day;");
            execute("create table eloverblik as (select timestamp_sequence_ns(0, 60 * 1000000000) ts, rnd_double() to_grid, rnd_double() from_grid from long_sequence(100)) timestamp(ts) partition by day;");

            assertQueryNoLeakCheck(
                    """
                            time\tsum\tsum1\tsum2
                            1970-01-01T00:00:00.000000000Z\t33.423793766512645\t28.964416248629917\t32.11038924761886
                            1970-01-01T01:00:00.000000000Z\t20.686394200400652\t18.863001213785466\t21.027598662521456
                            """,
                    """
                            SELECT a.ts as time, sum(a.to_grid), sum(a.from_grid), sum(b.hourly_production)
                            FROM 'eloverblik' as a, 'ap_systems' as b
                            WHERE a.ts = b.ts
                            SAMPLE BY 1h align to first observation
                            """,
                    "time"
            );

            assertQueryNoLeakCheck(
                    """
                            time\tsum\tsum1\tsum2
                            1970-01-01T00:00:00.000000000Z\t33.423793766512645\t28.964416248629917\t32.11038924761886
                            1970-01-01T01:00:00.000000000Z\t20.686394200400652\t18.863001213785466\t21.027598662521456
                            """,
                    """
                            SELECT a.ts as time, sum(a.to_grid), sum(a.from_grid), sum(b.hourly_production)
                            FROM 'eloverblik' as a, 'ap_systems' as b
                            WHERE a.ts = b.ts
                            SAMPLE BY 1h align to calendar
                            """,
                    "time",
                    true,
                    true
            );
        });
    }

    @Test
    public void testTimestampColumnAliasPosLast() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table ap_systems as (select timestamp_sequence_ns(0, 60 * 1000000000) ts, rnd_double() hourly_production from long_sequence(100)) timestamp(ts) partition by day;");
            execute("create table eloverblik as (select timestamp_sequence_ns(0, 60 * 1000000000) ts, rnd_double() to_grid, rnd_double() from_grid from long_sequence(100)) timestamp(ts) partition by day;");

            assertQueryNoLeakCheck(
                    """
                            sum\tsum1\tsum2\ttime
                            33.423793766512645\t28.964416248629917\t32.11038924761886\t1970-01-01T00:00:00.000000000Z
                            20.686394200400652\t18.863001213785466\t21.027598662521456\t1970-01-01T01:00:00.000000000Z
                            """,
                    """
                            SELECT sum(a.to_grid), sum(a.from_grid), sum(b.hourly_production), a.ts as time
                            FROM 'eloverblik' as a, 'ap_systems' as b
                            WHERE a.ts = b.ts
                            SAMPLE BY 1h ALIGN TO FIRST OBSERVATION
                            """,
                    "time"
            );

            assertQueryNoLeakCheck(
                    """
                            sum\tsum1\tsum2\ttime
                            33.423793766512645\t28.964416248629917\t32.11038924761886\t1970-01-01T00:00:00.000000000Z
                            20.686394200400652\t18.863001213785466\t21.027598662521456\t1970-01-01T01:00:00.000000000Z
                            """,
                    """
                            SELECT sum(a.to_grid), sum(a.from_grid), sum(b.hourly_production), a.ts as time
                            FROM 'eloverblik' as a, 'ap_systems' as b
                            WHERE a.ts = b.ts
                            SAMPLE BY 1h ALIGN TO CALENDAR
                            """,
                    "time",
                    true,
                    true
            );
        });
    }

    @Test
    public void testTimestampColumnAliasPosMid() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table ap_systems as (select timestamp_sequence_ns(0, 60 * 1000000000) ts, rnd_double() hourly_production from long_sequence(100)) timestamp(ts) partition by day;");
            execute("create table eloverblik as (select timestamp_sequence_ns(0, 60 * 1000000000) ts, rnd_double() to_grid, rnd_double() from_grid from long_sequence(100)) timestamp(ts) partition by day;");

            assertQueryNoLeakCheck(
                    """
                            sum\ttime\tsum1\tsum2
                            33.423793766512645\t1970-01-01T00:00:00.000000000Z\t28.964416248629917\t32.11038924761886
                            20.686394200400652\t1970-01-01T01:00:00.000000000Z\t18.863001213785466\t21.027598662521456
                            """,
                    """
                            SELECT sum(a.to_grid), a.ts as time, sum(a.from_grid), sum(b.hourly_production)
                            FROM 'eloverblik' as a, 'ap_systems' as b
                            WHERE a.ts = b.ts
                            SAMPLE BY 1h ALIGN TO FIRST OBSERVATION
                            """,
                    "time"
            );

            assertQueryNoLeakCheck(
                    """
                            sum\ttime\tsum1\tsum2
                            33.423793766512645\t1970-01-01T00:00:00.000000000Z\t28.964416248629917\t32.11038924761886
                            20.686394200400652\t1970-01-01T01:00:00.000000000Z\t18.863001213785466\t21.027598662521456
                            """,
                    """
                            SELECT sum(a.to_grid), a.ts as time, sum(a.from_grid), sum(b.hourly_production)
                            FROM 'eloverblik' as a, 'ap_systems' as b
                            WHERE a.ts = b.ts
                            SAMPLE BY 1h ALIGN TO CALENDAR
                            """,
                    "time",
                    true,
                    true
            );
        });
    }

    @Test
    public void testTimestampColumnJoinTableAliasFirst() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table ap_systems as (select timestamp_sequence_ns(0, 60 * 1000000000) ts, rnd_double() hourly_production from long_sequence(100)) timestamp(ts) partition by day;");
            execute("create table eloverblik as (select timestamp_sequence_ns(0, 60 * 1000000000) ts, rnd_double() to_grid, rnd_double() from_grid from long_sequence(100)) timestamp(ts) partition by day;");

            assertQueryNoLeakCheck(
                    """
                            ts\tsum\tsum1\tsum2
                            1970-01-01T00:00:00.000000000Z\t33.423793766512645\t28.964416248629917\t32.11038924761886
                            1970-01-01T01:00:00.000000000Z\t20.686394200400652\t18.863001213785466\t21.027598662521456
                            """,
                    """
                            SELECT a.ts, sum(a.to_grid), sum(a.from_grid), sum(b.hourly_production)
                            FROM 'eloverblik' as a, 'ap_systems' as b
                            WHERE a.ts = b.ts
                            SAMPLE BY 1h ALIGN TO FIRST OBSERVATION
                            """,
                    "ts"
            );

            assertQueryNoLeakCheck(
                    """
                            ts\tsum\tsum1\tsum2
                            1970-01-01T00:00:00.000000000Z\t33.423793766512645\t28.964416248629917\t32.11038924761886
                            1970-01-01T01:00:00.000000000Z\t20.686394200400652\t18.863001213785466\t21.027598662521456
                            """,
                    """
                            SELECT a.ts, sum(a.to_grid), sum(a.from_grid), sum(b.hourly_production)
                            FROM 'eloverblik' as a, 'ap_systems' as b
                            WHERE a.ts = b.ts
                            SAMPLE BY 1h ALIGN TO CALENDAR
                            """,
                    "ts",
                    true,
                    true
            );
        });
    }

    @Test
    public void testTimestampColumnJoinTableAliasLast() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table ap_systems as (select timestamp_sequence_ns(0, 60 * 1000000000) ts, rnd_double() hourly_production from long_sequence(100)) timestamp(ts) partition by day;");
            execute("create table eloverblik as (select timestamp_sequence_ns(0, 60 * 1000000000) ts, rnd_double() to_grid, rnd_double() from_grid from long_sequence(100)) timestamp(ts) partition by day;");

            assertQueryNoLeakCheck(
                    """
                            sum\tsum1\tsum2\tts
                            33.423793766512645\t28.964416248629917\t32.11038924761886\t1970-01-01T00:00:00.000000000Z
                            20.686394200400652\t18.863001213785466\t21.027598662521456\t1970-01-01T01:00:00.000000000Z
                            """,
                    """
                            SELECT sum(a.to_grid), sum(a.from_grid), sum(b.hourly_production), a.ts
                            FROM 'eloverblik' as a, 'ap_systems' as b
                            WHERE a.ts = b.ts
                            SAMPLE BY 1h ALIGN TO FIRST OBSERVATION
                            """,
                    "ts"
            );

            assertQueryNoLeakCheck(
                    """
                            sum\tsum1\tsum2\tts
                            33.423793766512645\t28.964416248629917\t32.11038924761886\t1970-01-01T00:00:00.000000000Z
                            20.686394200400652\t18.863001213785466\t21.027598662521456\t1970-01-01T01:00:00.000000000Z
                            """,
                    """
                            SELECT sum(a.to_grid), sum(a.from_grid), sum(b.hourly_production), a.ts
                            FROM 'eloverblik' as a, 'ap_systems' as b
                            WHERE a.ts = b.ts
                            SAMPLE BY 1h ALIGN TO CALENDAR
                            """,
                    "ts",
                    true,
                    true
            );
        });
    }

    @Test
    public void testTimestampColumnJoinTableAliasMid() throws Exception {
        assertMemoryLeak(() -> {
            execute("create table ap_systems as (select timestamp_sequence_ns(0, 60 * 1000000000) ts, rnd_double() hourly_production from long_sequence(100)) timestamp(ts) partition by day;");
            execute("create table eloverblik as (select timestamp_sequence_ns(0, 60 * 1000000000) ts, rnd_double() to_grid, rnd_double() from_grid from long_sequence(100)) timestamp(ts) partition by day;");

            assertQueryNoLeakCheck(
                    """
                            sum\tsum1\tsum2\ttime
                            33.423793766512645\t28.964416248629917\t32.11038924761886\t1970-01-01T00:00:00.000000000Z
                            20.686394200400652\t18.863001213785466\t21.027598662521456\t1970-01-01T01:00:00.000000000Z
                            """,
                    """
                            SELECT sum(a.to_grid), sum(a.from_grid), sum(b.hourly_production), a.ts as time
                            FROM 'eloverblik' as a, 'ap_systems' as b
                            WHERE a.ts = b.ts
                            SAMPLE BY 1h ALIGN TO FIRST OBSERVATION
                            """,
                    "time"
            );

            assertQueryNoLeakCheck(
                    """
                            sum\tsum1\tsum2\ttime
                            33.423793766512645\t28.964416248629917\t32.11038924761886\t1970-01-01T00:00:00.000000000Z
                            20.686394200400652\t18.863001213785466\t21.027598662521456\t1970-01-01T01:00:00.000000000Z
                            """,
                    """
                            SELECT sum(a.to_grid), sum(a.from_grid), sum(b.hourly_production), a.ts as time
                            FROM 'eloverblik' as a, 'ap_systems' as b
                            WHERE a.ts = b.ts
                            SAMPLE BY 1h ALIGN TO CALENDAR
                            """,
                    "time",
                    true,
                    true
            );
        });
    }

    @Test
    public void testTimestampFillNullAndValue() throws Exception {
        assertQuery(
                """
                        ts\tfirst\tlast
                        2021-03-28T00:00:00.000000000Z\t2021-03-28T01:59:00.000000000Z\t2021-03-28T01:59:00.000000000Z
                        2021-03-29T00:00:00.000000000Z\t\t2019-02-03T12:23:34.123456000Z
                        2021-03-30T00:00:00.000000000Z\t\t2019-02-03T12:23:34.123456000Z
                        2021-03-31T00:00:00.000000000Z\t2021-03-31T01:59:00.000000000Z\t2021-03-31T01:59:00.000000000Z
                        2021-04-01T00:00:00.000000000Z\t\t2019-02-03T12:23:34.123456000Z
                        2021-04-02T00:00:00.000000000Z\t\t2019-02-03T12:23:34.123456000Z
                        2021-04-03T00:00:00.000000000Z\t2021-04-03T01:59:00.000000000Z\t2021-04-03T01:59:00.000000000Z
                        2021-04-04T00:00:00.000000000Z\t\t2019-02-03T12:23:34.123456000Z
                        2021-04-05T00:00:00.000000000Z\t\t2019-02-03T12:23:34.123456000Z
                        2021-04-06T00:00:00.000000000Z\t2021-04-06T01:59:00.000000000Z\t2021-04-06T01:59:00.000000000Z
                        2021-04-07T00:00:00.000000000Z\t\t2019-02-03T12:23:34.123456000Z
                        2021-04-08T00:00:00.000000000Z\t\t2019-02-03T12:23:34.123456000Z
                        2021-04-09T00:00:00.000000000Z\t2021-04-09T01:59:00.000000000Z\t2021-04-09T01:59:00.000000000Z
                        2021-04-10T00:00:00.000000000Z\t\t2019-02-03T12:23:34.123456000Z
                        2021-04-11T00:00:00.000000000Z\t\t2019-02-03T12:23:34.123456000Z
                        2021-04-12T00:00:00.000000000Z\t2021-04-12T01:59:00.000000000Z\t2021-04-12T01:59:00.000000000Z
                        """,
                """
                        select ts, first(ts), last(ts)
                        from trade
                        sample by 1d fill(null, '2019-02-03T12:23:34.123456Z') align to CALENDAR;""", // oddly specific date to make sure it's parsed correctly up to microseconds
                "create table trade as (" +
                        "select timestamp_sequence_ns('2021-03-28T01:59:00.00000000Z', 3*24*3600*1000000000L) ts from long_sequence(6)" +
                        ") timestamp(ts)",
                "ts",
                false
        );
    }

    @Test
    public void testTimestampFillValueUnquoted() throws Exception {
        assertException(
                "select ts, first(ts), last(ts) " +
                        "from trade " +
                        "sample by 1d fill(null, 1236) align to CALENDAR;",
                "create table trade as (" +
                        "select timestamp_sequence_ns('2021-03-28T01:59:00.00000000Z', 3*24*3600*1000000000L) ts from long_sequence(6)" +
                        ") timestamp(ts)",
                66,
                "Invalid fill value: '1236'. Timestamp fill value must be in quotes."
        );
    }

    @Test
    public void testTimestampIsNotRequiredAfterSubqueryWithExplicitTs() throws Exception {
        assertQuery(
                """
                        ts\tvalue
                        2023-02-19T00:00:00.000000000Z\t0
                        2023-02-20T00:00:00.000000000Z\t0
                        2023-02-21T00:00:00.000000000Z\t0
                        2023-02-22T00:00:00.000000000Z\t0
                        2023-02-23T00:00:00.000000000Z\t0
                        2023-02-24T00:00:00.000000000Z\t0
                        2023-02-25T00:00:00.000000000Z\t0
                        2023-02-26T00:00:00.000000000Z\t0
                        2023-02-27T00:00:00.000000000Z\t0
                        2023-02-28T00:00:00.000000000Z\t0
                        2023-03-01T00:00:00.000000000Z\t1
                        """,
                """
                        WITH  all_rows    AS (
                            SELECT '2023-02-19T00:00:00.000000000Z'::timestamp_ns as ts, 'foobar' as address, 0 as value
                            union all
                            SELECT '2023-03-01T00:00:00.000000000Z'::timestamp_ns as ts, 'foobar' as address, 1 as value
                        ), just_foobar as (
                            select * from all_rows where address in ('foobar')
                        ), ordered as (
                            select * from just_foobar order by ts asc
                        ), timed as (
                            select * from ordered timestamp(ts)
                        ), sampled as (
                            SELECT ts, sum(value) as value
                            FROM timed
                            SAMPLE BY 1d FILL(0) ALIGN TO CALENDAR\s
                        )
                        select * from sampled;""",
                "ts"
        );
    }

    @Test
    public void testTimestampIsNotRequiredAfterSubqueryWithExplicitTs2() throws Exception {
        assertQuery(
                """
                        ts\tvalue
                        2023-02-19T00:00:00.000000000Z\t0
                        2023-02-20T00:00:00.000000000Z\t0
                        2023-02-21T00:00:00.000000000Z\t0
                        2023-02-22T00:00:00.000000000Z\t0
                        2023-02-23T00:00:00.000000000Z\t0
                        2023-02-24T00:00:00.000000000Z\t0
                        2023-02-25T00:00:00.000000000Z\t0
                        2023-02-26T00:00:00.000000000Z\t0
                        2023-02-27T00:00:00.000000000Z\t0
                        2023-02-28T00:00:00.000000000Z\t0
                        2023-03-01T00:00:00.000000000Z\t1
                        """,
                "WITH  all_rows    AS (\n" +
                        "    SELECT '2023-02-19T00:00:00.000000000Z'::timestamp_ns as ts, 'foobar' as address, 0 as value\n" +
                        "    union all\n" +
                        "    SELECT '2023-03-01T00:00:00.000000000Z'::timestamp_ns as ts, 'foobar' as address, 1 as value\n" +
                        "), just_foobar as (\n" +
                        "    select * from all_rows where address in ('foobar')\n" +
                        "), ordered as (\n" +
                        "    select * from just_foobar order by ts asc\n" +
                        "), timed as (\n" +
                        "    select * from ordered timestamp(ts)\n" +
                        "), intermediate as (\n" + // the distance between sample by and model with explicit ts is bigger
                        "    select * from timed where ts > 0::timestamp_ns \n" +
                        "), sampled as (\n" +
                        "    SELECT ts, sum(value) as value\n" +
                        "    FROM intermediate\n" +
                        "    SAMPLE BY 1d FILL(0) ALIGN TO CALENDAR \n" +
                        ")\n" +
                        "select * from sampled;",
                "ts"
        );
    }

    @Test
    public void testTimestampIsNotRequiredAfterSubqueryWithExplicitTsNotInSelectList() throws Exception {
        assertQuery(
                """
                        value
                        0
                        0
                        0
                        0
                        0
                        0
                        0
                        0
                        0
                        0
                        1
                        """,
                "WITH  all_rows    AS (\n" +
                        "    SELECT '2023-02-19T00:00:00.000000000Z'::timestamp_ns as ts, 'foobar' as address, 0 as value\n" +
                        "    union all\n" +
                        "    SELECT '2023-03-01T00:00:00.000000000Z'::timestamp_ns as ts, 'foobar' as address, 1 as value\n" +
                        "), just_foobar as (\n" +
                        "    select * from all_rows where address in ('foobar')\n " +
                        "), ordered as (\n" +
                        "    select * from just_foobar order by ts asc\n" +
                        "), timed as (\n" +
                        "    select * from ordered timestamp(ts)\n" +
                        "), sampled as (\n" +
                        "    SELECT sum(value) as value\n" + // no ts in select list
                        "    FROM timed\n" +
                        "    SAMPLE BY 1d FILL(0) ALIGN TO CALENDAR \n" +
                        ")\n" +
                        "select * from sampled;",
                null
        );
    }

    @Test
    public void testTimestampIsNotRequiredInFilterSubQuery() throws Exception {
        // (x union x) is used in sub-query to make sure that the base doesn't have designated timestamp
        assertQuery(
                """
                        sym\tv
                        baz\t7
                        """,
                """
                        select sym, last(value) v
                        from x
                        where sym in (select sym from (x union x) where sym in ('baz'))
                        sample by 1d align to first observation\s""",
                """
                        create table x as (
                          select x as value,
                                 rnd_symbol('foo','bar','baz') sym,
                                 cast(x as timestamp_ns) ts
                          from long_sequence(10)
                        ) timestamp(ts) partition by day""",
                null,
                false
        );
    }

    @Test
    public void testTimestampIsRequiredBeforeSubqueryWithExplicitTs1() throws Exception {
        assertException(
                """
                        WITH  all_rows    AS (
                            SELECT '2023-02-19T00:00:00.000000000Z'::timestamp_ns as ts, 'foobar' as address, 0 as value
                            union all
                            SELECT '2023-03-01T00:00:00.000000000Z'::timestamp_ns as ts, 'foobar' as address, 1 as value
                        ), just_foobar as (
                            select * from all_rows where address in ('foobar')
                        ), ordered as (
                            select * from just_foobar order by ts asc
                        ), intermediate as (
                            select * from ordered timestamp(ts) \
                            union all \
                            select '2023-02-01T00:00:00.000000000Z'::timestamp_ns, 'f', 2\s
                        ), sampled as (
                            SELECT ts, sum(value) as value
                            FROM intermediate
                            SAMPLE BY 1d FILL(0) ALIGN TO CALENDAR\s
                        )
                        select * from sampled;""",
                525,
                "base query does not provide ASC order over designated TIMESTAMP column"
        );
    }

    @Test
    public void testTimestampIsRequiredBeforeSubqueryWithExplicitTs2() throws Exception {
        assertException(
                """
                        WITH  all_rows    AS (
                            SELECT '2023-02-19T00:00:00.000000000Z'::timestamp_ns as ts, 'foobar' as address, 0 as value
                            union all
                            SELECT '2023-03-01T00:00:00.000000000Z'::timestamp_ns as ts, 'foobar' as address, 1 as value
                        ), just_foobar as (
                            select * from all_rows where address in ('foobar')
                        ), ordered as (
                            select * from just_foobar order by ts asc
                        ), with_ts as (
                            select * from ordered timestamp(ts) \
                        ), intermediate as (
                            select * from with_ts order by value \
                        ), sampled as (
                            SELECT ts, sum(value) as value
                            FROM intermediate
                            SAMPLE BY 1d FILL(0) ALIGN TO CALENDAR\s
                        )
                        select * from sampled;""",
                501,
                "base query does not provide ASC order over designated TIMESTAMP column"
        );
    }

    @Test
    public void testTimestampSpecifiedForTableWithNoDesignatedTimestamp() throws Exception {
        assertQuery(
                """
                        ts\tv
                        1970-01-01T00:00:00.000001000Z\t10
                        """,
                """
                        select ts, last(value) v
                        from (
                            select ts, value
                            from x
                            where sym is not null
                            order by ts
                        ) timestamp(ts)
                        sample by 1d align to first observation""",
                """
                        create table x as (
                          select x as value,
                                 rnd_symbol(100, 10, 10, 0) sym,
                                 cast(x*1000 as timestamp_ns) ts
                          from long_sequence(10)
                        )""",
                "ts",
                false
        );
    }

    @Test
    public void testUuidFillNull() throws Exception {
        assertQuery(
                """
                        s\tk\tfirst\tlast
                        TJW\t1970-01-03T00:00:00.000000000Z\t797fa69e-b8fe-46cc-a8be-ef38cd7bb3d8\t797fa69e-b8fe-46cc-a8be-ef38cd7bb3d8
                        TJW\t1970-01-03T00:30:00.000000000Z\t\t
                        TJW\t1970-01-03T01:00:00.000000000Z\tc72bfc52-3015-4059-980e-ca62a219a0f1\tc72bfc52-3015-4059-980e-ca62a219a0f1
                        """,
                "select s, k, " +
                        "first(u), " +
                        "last(u) " +
                        "from x sample by 30m fill(NULL)",
                "create table x as " +
                        "(" +
                        "select" +
                        " rnd_uuid4() u," +
                        " rnd_symbol(2,3,4,0) s, " +
                        " timestamp_sequence_ns(172800000000000, 3600000000000) k" +
                        " from" +
                        " long_sequence(2)" +
                        ") timestamp(k) partition by NONE",
                "k",
                false
        );
    }

    @Test
    public void testWrongTypeInPeriodSyntax() throws Exception {
        testSampleByPeriodFails(
                "select k, s, first(lat) lat, last(lon) lon from x where s in ('a') sample by 1.0*3 T",
                "select k, s, first(lat) lat, last(lon) lon from x where s in ('a') sample by 1.0*".length() - 1,
                "sample by period must be a constant expression of INT or LONG type"
        );
    }

    @Test
    public void testWrongTypeInPeriodSyntax2() throws Exception {
        testSampleByPeriodFails(
                "select k, s, first(lat) lat, last(lon) lon from x where s in ('a') sample by '1T' align to first observation",
                "select k, s, first(lat) lat, last(lon) lon from x where s in ('a') sample by '".length() - 1,
                "expected single letter qualifier"
        );
    }

    @Test
    public void testWrongTypeInPeriodSyntax3() throws Exception {
        testSampleByPeriodFails(
                "select k, s, first(lat) lat, last(lon) lon from x where s in ('a') sample by '1' T",
                "select k, s, first(lat) lat, last(lon) lon from x where s in ('a') sample by '1' T".length() - 1,
                "unexpected token [T]"
        );
    }

    @Test
    public void testWrongTypeInUnit() throws Exception {
        testSampleByPeriodFails(
                "select k, s, first(lat) lat, last(lon) lon from x where s in ('a') sample by 10*3 mi",
                "select k, s, first(lat) lat, last(lon) lon from x where s in ('a') sample by 10*3 m".length() - 1,
                "one letter sample by period unit expected"
        );
    }

    @NotNull
    private static CairoConfiguration createMmapFailingConfiguration(int x) {
        FilesFacade ff = new TestFilesFacadeImpl() {
            int count = x;

            @Override
            public long mmap(long fd, long len, long offset, int flags, int memoryTag) {
                if (count-- > 0) {
                    return super.mmap(fd, len, offset, flags, memoryTag);
                }
                return -1;
            }
        };

        return new DefaultTestCairoConfiguration(root) {
            @Override
            public @NotNull FilesFacade getFilesFacade() {
                return ff;
            }
        };
    }

    private void assertSampleByFlavours(String expected, String sql) throws SqlException {
        assertSql(expected, sql);
        assertSql(expected, sql + " ALIGN TO FIRST OBSERVATION;");
    }

    private void assertSampleByIndexQuery(String expected, String query, String insert) throws Exception {
        assertSampleByIndexQuery(expected, query, insert, false, false);
    }

    private void assertSampleByIndexQuery(String expected, String query, String insert, boolean supportsRandomAccess, boolean expectSize) throws Exception {
        assertMemoryLeak(() -> {
            String forceNoIndexQuery = query.replace("in ('b')", "in ('b', 'none')")
                    .replace("in ('a')", "in ('a', 'none')");

            assertQueryNoLeakCheck(
                    expected,
                    forceNoIndexQuery,
                    insert,
                    "k",
                    supportsRandomAccess,
                    expectSize
            );

            assertQueryNoLeakCheck(
                    expected,
                    query,
                    null,
                    "k",
                    supportsRandomAccess,
                    expectSize
            );
        });
    }

    private void assertWithSymbolColumnTop(String expected, String query) throws Exception {
        assertWithSymbolColumnTop(expected, query, false, false);
    }

    private void assertWithSymbolColumnTop(String expected, String query, boolean supportsRandomAccess, boolean expectSize) throws Exception {
        assertMemoryLeak(() -> {
            execute("alter table xx drop column s", sqlExecutionContext);
            execute("alter table xx add s SYMBOL INDEX", sqlExecutionContext);

            String forceNoIndexQuery = query.replace("and s = null", " ");

            assertQueryNoLeakCheck(
                    expected,
                    forceNoIndexQuery,
                    null,
                    "k",
                    supportsRandomAccess,
                    expectSize
            );

            assertQueryNoLeakCheck(
                    expected,
                    query,
                    null,
                    "k",
                    supportsRandomAccess,
                    expectSize
            );
        });
    }

    private boolean isNone(String fill) {
        return "".equals(fill) || "none".equals(fill);
    }

    private void testSampleByFirstLastIndexedConcurrent(String query) throws Exception {
        // This test verifies that the native code does not access unmapped memory
        // when queries are run concurrently with ingestion.

        final int threadCount = 4;
        final int workerCount = 2;

        try (WorkerPool pool = new WorkerPool(() -> workerCount)) {
            assertMemoryLeak(() -> TestUtils.execute(
                    pool,
                    (engine, compiler, sqlExecutionContext) -> {
                        engine.execute(
                                "create table x (d1 double, d2 double, s symbol index, kms long, k timestamp_ns) timestamp(k) partition by day;",
                                sqlExecutionContext
                        );

                        final RecordCursorFactory[] factories = new RecordCursorFactory[threadCount];
                        for (int i = 0; i < threadCount; i++) {
                            factories[i] = engine.select(query, sqlExecutionContext);
                        }

                        final AtomicInteger errors = new AtomicInteger();
                        final CyclicBarrier barrier = new CyclicBarrier(threadCount);
                        final SOCountDownLatch haltLatch = new SOCountDownLatch(threadCount);
                        final AtomicBoolean writerDone = new AtomicBoolean();
                        for (int i = 0; i < threadCount; i++) {
                            final int finalI = i;
                            new Thread(() -> {
                                TestUtils.await(barrier);

                                try {
                                    final RecordCursorFactory factory = factories[finalI];
                                    while (!writerDone.get()) {
                                        try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                                            TestUtils.drainCursor(cursor);
                                        } catch (Throwable e) {
                                            e.printStackTrace();
                                            errors.incrementAndGet();
                                        }
                                    }
                                    haltLatch.countDown();
                                } finally {
                                    Path.clearThreadLocals();
                                }
                            }).start();
                        }

                        final int rows = 10000;
                        final int batchSize = 10;
                        long ts = 0;
                        try (TableWriter writer = TestUtils.getWriter(engine, "x")) {
                            for (int i = 0; i < rows; i++) {
                                TableWriter.Row row = writer.newRow(ts);
                                row.putDouble(0, 42);
                                row.putDouble(1, 42);
                                row.putSym(2, (char) ('a' + i % 3));
                                ts += Nanos.SECOND_NANOS;
                                row.putLong(3, ts / Nanos.MILLI_NANOS);
                                row.append();
                                if ((i % batchSize) == 0) {
                                    writer.commit();
                                }
                            }
                            writer.commit();
                        }

                        writerDone.set(true);
                        haltLatch.await();

                        Misc.free(factories);
                        Assert.assertEquals(0, errors.get());
                    },
                    configuration,
                    LOG
            ));
        }
    }

    private void testSampleByPeriodFails(String query, int errorPosition, String errorContains) throws Exception {
        assertMemoryLeak(() -> {
            execute(
                    "create table x as " +
                            "(" +
                            "select" +
                            "   rnd_double(1)*180 lat," +
                            "   rnd_double(1)*180 lon," +
                            "   rnd_symbol('a') s," +
                            "   timestamp_sequence_ns('2021-03-28T00:59:00.000000000Z', 60*1000000L) k" +
                            "   from" +
                            "   long_sequence(100)" +
                            "), index(s) timestamp(k) partition by DAY"
            );
            try {
                assertExceptionNoLeakCheck(query);
            } catch (SqlException ex) {
                TestUtils.assertContains(ex.getFlyweightMessage(), errorContains);
                Assert.assertEquals(errorPosition, ex.getPosition());
            }
        });
    }

    private void testSampleByPushdown(String fill, String alignTo, String plan) throws Exception {
        testSampleByPushdownWithDesignatedTs(fill, alignTo, plan);
        testSampleByPushdownWithoutDesignatedTs(fill, alignTo, plan);
    }

    private void testSampleByPushdownWithDesignatedTs(String fill, String alignTo, String plan) throws Exception {
        assertMemoryLeak(() -> {
            execute("create table if not exists x (  ts timestamp_ns, sym symbol, val long ) timestamp(ts) partition by DAY");
            String fillOpt = fill.isEmpty() ? "" : "fill(" + fill + ")";
            String query = "select * from (" +
                    "select ts as tstmp, sym, first(val), avg(val), last(val), max(val) " +
                    "from x " +
                    "sample by 1m " + fillOpt + " " + alignTo + " ) " +
                    "where tstmp >= '2022-12-01T00:00:00.000000000Z' and  sym = 'B' and length(sym)*tstmp::long > 0  ";
            String actualPlan = plan.replace("#TABLE#", "x");
            assertPlanNoLeakCheck(query, actualPlan);
        });
    }

    private void testSampleByPushdownWithoutDesignatedTs(String fill, String alignTo, String plan) throws Exception {
        assertMemoryLeak(() -> {
            execute("create table if not exists y (  ts timestamp_ns, sym symbol, val long ) ");
            String fillOpt = fill.isEmpty() ? "" : "fill(" + fill + ")";
            String query = "select * from (" +
                    "select ts as tstmp, sym, first(val), avg(val), last(val), max(val) " +
                    "from y timestamp(ts) " +
                    "sample by 1m " + fillOpt + " " + alignTo + " ) " +
                    "where tstmp >= '2022-12-01T00:00:00.000000000Z' and  sym = 'B' and length(sym)*tstmp::long > 0  ";
            String actualPlan = plan.replace("#TABLE#", "y");
            assertPlanNoLeakCheck(query, actualPlan);
        });
    }
}
